1: <?php
2:
3: namespace NNV\OneSignal\API;
4:
5: use NNV\OneSignal\OneSignal;
6: use NNV\OneSignal\Utils\Validation;
7:
8: 9: 10:
11: class App
12: {
13: 14: 15: 16: 17:
18: private $oneSignal;
19:
20: 21: 22:
23: public function __construct(OneSignal $oneSignal)
24: {
25: $this->oneSignal = $oneSignal;
26: }
27:
28: 29: 30: 31: 32:
33: public function all()
34: {
35: $apps = $this->oneSignal->execute('apps', 'GET');
36:
37: return $apps;
38: }
39:
40: 41: 42: 43: 44: 45:
46: public function get($id)
47: {
48: $url = sprintf('apps/%s', $id);
49: $app = $this->oneSignal->execute($url, 'GET');
50:
51: return $app;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function create(array $appData)
61: {
62: $this->validateAppData($appData, $this->getAppDataRules());
63:
64: $app = $this->oneSignal->execute('apps', 'POST', [
65: 'form_params' => $appData,
66: ]);
67:
68: return $app;
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: public function update($appId, array $appData)
79: {
80: $appDataRules = $this->getAppDataRules();
81: unset($appDataRules['required'][0]);
82:
83: $this->validateAppData($appData, $appDataRules);
84:
85: $url = sprintf('apps/%s', $appId);
86: $app = $this->oneSignal->execute($url, 'PUT', [
87: 'form_params' => $appData,
88: ]);
89:
90: return $app;
91: }
92:
93: 94: 95: 96: 97:
98: private function getAppDataRules()
99: {
100: return [
101: 'required' => ['name'],
102: 'defined' => [
103: 'name' => 'string',
104: 'apns_env' => [
105: 'allowedTypes' => 'string',
106: 'allowedValues' => ['sandbox', 'production'],
107: ],
108: 'apns_p12' => 'string',
109: 'apns_p12_password' => 'string',
110: 'gcm_key' => 'string',
111: 'android_gcm_sender_id' => 'string',
112: 'chrome_web_origin' => 'string',
113: 'chrome_web_origin' => 'string',
114: 'chrome_web_default_notification_icon' => 'string',
115: 'chrome_web_sub_domain' => 'string',
116: 'safari_apns_p12' => 'string',
117: 'safari_apns_p12_password' => 'string',
118: 'site_name' => 'string',
119: 'safari_site_origin' => 'string',
120: 'safari_icon_256_256' => 'string',
121: 'chrome_key' => 'string',
122: ],
123: ];
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: private function validateAppData(array $appData, array $validateRules)
134: {
135: $validation = new Validation;
136:
137: $validation->setMultiRequired($validateRules['required'])
138: ->setMultiDefined($validateRules['defined'])
139: ->validate($appData);
140: }
141: }
142: