|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OneSignal; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
|
|
8
|
|
|
class Notifications |
|
9
|
|
|
{ |
|
10
|
|
|
const NOTIFICATIONS_LIMIT = 50; |
|
11
|
|
|
|
|
12
|
|
|
protected $api; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(OneSignal $api) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->api = $api; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get information about notification with provided ID. |
|
21
|
|
|
* |
|
22
|
|
|
* Application authentication key and ID must be set. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $id Notification ID |
|
25
|
|
|
* |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getOne($id) |
|
29
|
|
|
{ |
|
30
|
|
|
$url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId(); |
|
31
|
|
|
|
|
32
|
|
|
return $this->api->request('GET', $url, [ |
|
33
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get information about all notifications. |
|
39
|
|
|
* |
|
40
|
|
|
* Application authentication key and ID must be set. |
|
41
|
|
|
* |
|
42
|
|
|
* @param int $limit How many notifications to return (max 50) |
|
43
|
|
|
* @param int $offset Results offset (results are sorted by ID) |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function getAll($limit = self::NOTIFICATIONS_LIMIT, $offset = 0) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
return $this->api->request('GET', '/notifications?'.http_build_query([ |
|
50
|
|
|
'limit' => max(0, min(self::NOTIFICATIONS_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))), |
|
51
|
|
|
'offset' => max(0, min(self::NOTIFICATIONS_LIMIT, filter_var($offset, FILTER_VALIDATE_INT))), |
|
52
|
|
|
]), [ |
|
53
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
|
54
|
|
|
'Content-Type' => 'application/json', |
|
55
|
|
|
], json_encode([ |
|
56
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
|
57
|
|
|
])); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Send new notification with provided data. |
|
62
|
|
|
* |
|
63
|
|
|
* Application authentication key and ID must be set. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $data |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
public function add(array $data) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
return $this->api->request('POST', '/notifications', [ |
|
72
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
|
73
|
|
|
'Content-Type' => 'application/json', |
|
74
|
|
|
], json_encode($this->resolve($data))); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Open notification. |
|
79
|
|
|
* |
|
80
|
|
|
* Application authentication key and ID must be set. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $id Notification ID |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function open($id) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
return $this->api->request('PUT', '/notifications/'.$id, [ |
|
89
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
|
90
|
|
|
'Content-Type' => 'application/json', |
|
91
|
|
|
], json_encode([ |
|
92
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
|
93
|
|
|
'opened' => true, |
|
94
|
|
|
])); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Cancel notification. |
|
99
|
|
|
* |
|
100
|
|
|
* Application authentication key and ID must be set. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $id Notification ID |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function cancel($id) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
return $this->api->request('DELETE', '/notifications/'.$id, [ |
|
109
|
|
|
'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(), |
|
110
|
|
|
'Content-Type' => 'application/json', |
|
111
|
|
|
], json_encode([ |
|
112
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
|
113
|
|
|
])); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function resolve(array $data) |
|
117
|
|
|
{ |
|
118
|
|
|
$resolver = new OptionsResolver(); |
|
119
|
|
|
|
|
120
|
|
|
$resolver |
|
121
|
|
|
->setDefined('contents') |
|
122
|
|
|
->setAllowedTypes('contents', 'array') |
|
123
|
|
|
->setDefined('headings') |
|
124
|
|
|
->setAllowedTypes('headings', 'array') |
|
125
|
|
|
->setDefined('subtitle') |
|
126
|
|
|
->setAllowedTypes('subtitle', 'array') |
|
127
|
|
|
->setDefined('isIos') |
|
128
|
|
|
->setAllowedTypes('isIos', 'bool') |
|
129
|
|
|
->setDefined('isAndroid') |
|
130
|
|
|
->setAllowedTypes('isAndroid', 'bool') |
|
131
|
|
|
->setDefined('isWP') |
|
132
|
|
|
->setAllowedTypes('isWP', 'bool') |
|
133
|
|
|
->setDefined('isWP_WNS') |
|
134
|
|
|
->setAllowedTypes('isWP_WNS', 'bool') |
|
135
|
|
|
->setDefined('isAdm') |
|
136
|
|
|
->setAllowedTypes('isAdm', 'bool') |
|
137
|
|
|
->setDefined('isChrome') |
|
138
|
|
|
->setAllowedTypes('isChrome', 'bool') |
|
139
|
|
|
->setDefined('isChromeWeb') |
|
140
|
|
|
->setAllowedTypes('isChromeWeb', 'bool') |
|
141
|
|
|
->setDefined('isFirefox') |
|
142
|
|
|
->setAllowedTypes('isFirefox', 'bool') |
|
143
|
|
|
->setDefined('isSafari') |
|
144
|
|
|
->setAllowedTypes('isSafari', 'bool') |
|
145
|
|
|
->setDefined('isAnyWeb') |
|
146
|
|
|
->setAllowedTypes('isAnyWeb', 'bool') |
|
147
|
|
|
->setDefined('included_segments') |
|
148
|
|
|
->setAllowedTypes('included_segments', 'array') |
|
149
|
|
|
->setDefined('excluded_segments') |
|
150
|
|
|
->setAllowedTypes('excluded_segments', 'array') |
|
151
|
|
|
->setDefined('include_player_ids') |
|
152
|
|
|
->setAllowedTypes('include_player_ids', 'array') |
|
153
|
|
|
->setDefined('include_ios_tokens') |
|
154
|
|
|
->setAllowedTypes('include_ios_tokens', 'array') |
|
155
|
|
|
->setDefined('include_android_reg_ids') |
|
156
|
|
|
->setAllowedTypes('include_android_reg_ids', 'array') |
|
157
|
|
|
->setDefined('include_wp_uris') |
|
158
|
|
|
->setAllowedTypes('include_wp_uris', 'array') |
|
159
|
|
|
->setDefined('include_wp_wns_uris') |
|
160
|
|
|
->setAllowedTypes('include_wp_wns_uris', 'array') |
|
161
|
|
|
->setDefined('include_amazon_reg_ids') |
|
162
|
|
|
->setAllowedTypes('include_amazon_reg_ids', 'array') |
|
163
|
|
|
->setDefined('include_chrome_reg_ids') |
|
164
|
|
|
->setAllowedTypes('include_chrome_reg_ids', 'array') |
|
165
|
|
|
->setDefined('include_chrome_web_reg_ids') |
|
166
|
|
|
->setAllowedTypes('include_chrome_web_reg_ids', 'array') |
|
167
|
|
|
->setDefined('app_ids') |
|
168
|
|
|
->setAllowedTypes('app_ids', 'array') |
|
169
|
|
|
->setDefined('filters') |
|
170
|
|
|
->setAllowedTypes('filters', 'array') |
|
171
|
|
|
->setNormalizer('filters', function (Options $options, array $value) { |
|
172
|
|
|
$filters = []; |
|
173
|
|
|
|
|
174
|
|
|
foreach ($value as $filter) { |
|
175
|
|
|
if (isset($filter['field'], $filter['key'], $filter['relation'], $filter['value'])) { |
|
176
|
|
|
$filters[] = [ |
|
177
|
|
|
'field' => (string) $filter['field'], |
|
178
|
|
|
'key' => (string) $filter['key'], |
|
179
|
|
|
'relation' => (string) $filter['relation'], |
|
180
|
|
|
'value' => $filter['value'], |
|
181
|
|
|
]; |
|
182
|
|
|
} elseif (isset($filter['operator'])) { |
|
183
|
|
|
$filters[] = ['operator' => 'OR']; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $filters; |
|
188
|
|
|
}) |
|
189
|
|
|
->setDefined('tags') |
|
190
|
|
|
->setAllowedTypes('tags', 'array') |
|
191
|
|
|
->setNormalizer('tags', function (Options $options, array $value) { |
|
192
|
|
|
$tags = []; |
|
193
|
|
|
|
|
194
|
|
|
foreach ($value as $tag) { |
|
195
|
|
|
if (isset($tag['key'], $tag['relation'], $tag['value'])) { |
|
196
|
|
|
$tags[] = [ |
|
197
|
|
|
'key' => (string) $tag['key'], |
|
198
|
|
|
'relation' => (string) $tag['relation'], |
|
199
|
|
|
'value' => (string) $tag['value'], |
|
200
|
|
|
]; |
|
201
|
|
|
} elseif (isset($tag['operator'])) { |
|
202
|
|
|
$tags[] = ['operator' => 'OR']; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $tags; |
|
207
|
|
|
}) |
|
208
|
|
|
->setDefined('ios_badgeType') |
|
209
|
|
|
->setAllowedTypes('ios_badgeType', 'string') |
|
210
|
|
|
->setAllowedValues('ios_badgeType', ['None', 'SetTo', 'Increase']) |
|
211
|
|
|
->setDefined('ios_badgeCount') |
|
212
|
|
|
->setAllowedTypes('ios_badgeCount', 'int') |
|
213
|
|
|
->setDefined('ios_sound') |
|
214
|
|
|
->setAllowedTypes('ios_sound', 'string') |
|
215
|
|
|
->setDefined('android_sound') |
|
216
|
|
|
->setAllowedTypes('android_sound', 'string') |
|
217
|
|
|
->setDefined('adm_sound') |
|
218
|
|
|
->setAllowedTypes('adm_sound', 'string') |
|
219
|
|
|
->setDefined('wp_sound') |
|
220
|
|
|
->setAllowedTypes('wp_sound', 'string') |
|
221
|
|
|
->setDefined('wp_wns_sound') |
|
222
|
|
|
->setAllowedTypes('wp_wns_sound', 'string') |
|
223
|
|
|
->setDefined('data') |
|
224
|
|
|
->setAllowedTypes('data', 'array') |
|
225
|
|
|
->setDefined('buttons') |
|
226
|
|
|
->setAllowedTypes('buttons', 'array') |
|
227
|
|
|
->setNormalizer('buttons', function (Options $options, array $value) { |
|
228
|
|
|
$buttons = []; |
|
229
|
|
|
|
|
230
|
|
|
foreach ($value as $button) { |
|
231
|
|
|
if (!isset($button['text'])) { |
|
232
|
|
|
continue; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$buttons[] = [ |
|
236
|
|
|
'id' => (isset($button['id']) ? $button['id'] : mt_rand()), |
|
237
|
|
|
'text' => $button['text'], |
|
238
|
|
|
'icon' => (isset($button['icon']) ? $button['icon'] : null), |
|
239
|
|
|
]; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $buttons; |
|
243
|
|
|
}) |
|
244
|
|
|
->setDefined('android_background_layout') |
|
245
|
|
|
->setAllowedValues('android_background_layout', 'array') |
|
246
|
|
|
->setAllowedValues('android_background_layout', function ($layout) { |
|
247
|
|
|
if (empty($layout)) { |
|
248
|
|
|
return false; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$requiredKeys = ['image', 'headings_color', 'contents_color']; |
|
252
|
|
|
|
|
253
|
|
|
foreach ($layout as $k => $v) { |
|
254
|
|
|
if (!in_array($k, $requiredKeys) || !is_string($v)) { |
|
255
|
|
|
return false; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
return true; |
|
260
|
|
|
}) |
|
261
|
|
|
->setDefined('small_icon') |
|
262
|
|
|
->setAllowedTypes('small_icon', 'string') |
|
263
|
|
|
->setDefined('large_icon') |
|
264
|
|
|
->setAllowedTypes('large_icon', 'string') |
|
265
|
|
|
->setDefined('ios_attachments') |
|
266
|
|
|
->setAllowedTypes('ios_attachments', 'array') |
|
267
|
|
|
->setAllowedValues('ios_attachments', function ($attachments) { |
|
268
|
|
|
foreach ($attachments as $key => $value) { |
|
269
|
|
|
if (!is_string($key) || !is_string($value)) { |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return true; |
|
275
|
|
|
}) |
|
276
|
|
|
->setDefined('big_picture') |
|
277
|
|
|
->setAllowedTypes('big_picture', 'string') |
|
278
|
|
|
->setDefined('adm_small_icon') |
|
279
|
|
|
->setAllowedTypes('adm_small_icon', 'string') |
|
280
|
|
|
->setDefined('adm_large_icon') |
|
281
|
|
|
->setAllowedTypes('adm_large_icon', 'string') |
|
282
|
|
|
->setDefined('adm_big_picture') |
|
283
|
|
|
->setAllowedTypes('adm_big_picture', 'string') |
|
284
|
|
|
->setDefined('web_buttons') |
|
285
|
|
|
->setAllowedTypes('web_buttons', 'array') |
|
286
|
|
|
->setAllowedValues('web_buttons', function ($buttons) { |
|
287
|
|
|
$requiredKeys = ['id', 'text', 'icon', 'url']; |
|
288
|
|
|
foreach ($buttons as $button) { |
|
289
|
|
|
if (!is_array($button)) { |
|
290
|
|
|
return false; |
|
291
|
|
|
} |
|
292
|
|
|
if (count(array_intersect_key(array_flip($requiredKeys), $button)) != count($requiredKeys)) { |
|
293
|
|
|
return false; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return true; |
|
298
|
|
|
}) |
|
299
|
|
|
->setDefined('ios_category') |
|
300
|
|
|
->setAllowedTypes('ios_category', 'string') |
|
301
|
|
|
->setDefined('chrome_icon') |
|
302
|
|
|
->setAllowedTypes('chrome_icon', 'string') |
|
303
|
|
|
->setDefined('chrome_big_picture') |
|
304
|
|
|
->setAllowedTypes('chrome_big_picture', 'string') |
|
305
|
|
|
->setDefined('chrome_web_icon') |
|
306
|
|
|
->setAllowedTypes('chrome_web_icon', 'string') |
|
307
|
|
|
->setDefined('firefox_icon') |
|
308
|
|
|
->setAllowedTypes('firefox_icon', 'string') |
|
309
|
|
|
->setDefined('url') |
|
310
|
|
|
->setAllowedTypes('url', 'string') |
|
311
|
|
|
->setAllowedValues('url', function ($value) { |
|
312
|
|
|
return (bool) filter_var($value, FILTER_VALIDATE_URL); |
|
313
|
|
|
}) |
|
314
|
|
|
->setDefined('send_after') |
|
315
|
|
|
->setAllowedTypes('send_after', ['\DateTime', '\DateTimeInterface']) |
|
316
|
|
|
->setNormalizer('send_after', function (Options $options, \DateTime $value) { |
|
317
|
|
|
//Fri May 02 2014 00:00:00 GMT-0700 (PDT) |
|
|
|
|
|
|
318
|
|
|
return $value->format('D M d Y H:i:s eO (T)'); |
|
319
|
|
|
}) |
|
320
|
|
|
->setDefined('delayed_option') |
|
321
|
|
|
->setAllowedTypes('delayed_option', 'string') |
|
322
|
|
|
->setAllowedValues('delayed_option', ['timezone', 'last-active']) |
|
323
|
|
|
->setDefined('delivery_time_of_day') |
|
324
|
|
|
->setAllowedTypes('delivery_time_of_day', ['\DateTime', '\DateTimeInterface']) |
|
325
|
|
|
->setNormalizer('delivery_time_of_day', function (Options $options, \DateTime $value) { |
|
326
|
|
|
return $value->format('g:iA'); |
|
327
|
|
|
}) |
|
328
|
|
|
->setDefined('android_led_color') |
|
329
|
|
|
->setAllowedTypes('android_led_color', 'string') |
|
330
|
|
|
->setDefined('android_accent_color') |
|
331
|
|
|
->setAllowedTypes('android_accent_color', 'string') |
|
332
|
|
|
->setDefined('android_visibility') |
|
333
|
|
|
->setAllowedTypes('android_visibility', 'int') |
|
334
|
|
|
->setAllowedValues('android_visibility', [-1, 0, 1]) |
|
335
|
|
|
->setDefined('content_available') |
|
336
|
|
|
->setAllowedTypes('content_available', 'bool') |
|
337
|
|
|
->setDefined('mutable_content') |
|
338
|
|
|
->setAllowedTypes('mutable_content', 'bool') |
|
339
|
|
|
->setDefined('android_background_data') |
|
340
|
|
|
->setAllowedTypes('android_background_data', 'bool') |
|
341
|
|
|
->setDefined('amazon_background_data') |
|
342
|
|
|
->setAllowedTypes('amazon_background_data', 'bool') |
|
343
|
|
|
->setDefined('template_id') |
|
344
|
|
|
->setAllowedTypes('template_id', 'string') |
|
345
|
|
|
->setDefined('android_group') |
|
346
|
|
|
->setAllowedTypes('android_group', 'string') |
|
347
|
|
|
->setDefined('android_group_message') |
|
348
|
|
|
->setAllowedTypes('android_group_message', 'array') |
|
349
|
|
|
->setDefined('adm_group') |
|
350
|
|
|
->setAllowedTypes('adm_group', 'string') |
|
351
|
|
|
->setDefined('adm_group_message') |
|
352
|
|
|
->setAllowedTypes('adm_group_message', 'array') |
|
353
|
|
|
->setDefined('ttl') |
|
354
|
|
|
->setAllowedTypes('ttl', 'int') |
|
355
|
|
|
->setDefined('priority') |
|
356
|
|
|
->setAllowedTypes('priority', 'int') |
|
357
|
|
|
->setDefault('app_id', $this->api->getConfig()->getApplicationId()); |
|
358
|
|
|
|
|
359
|
|
|
return $resolver->resolve($data); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.