This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the MobileNotif package. |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | namespace LinkValue\MobileNotif\Model; |
||
11 | |||
12 | /** |
||
13 | * Google Cloud Messaging Message implementation. |
||
14 | * |
||
15 | * Refer to GCM documentation for more details. |
||
16 | * |
||
17 | * @see https://developers.google.com/cloud-messaging/http-server-ref |
||
18 | * |
||
19 | * @author Jamal Youssefi <[email protected]> |
||
20 | * @author Valentin Coulon <[email protected]> |
||
21 | */ |
||
22 | class GcmMessage extends Message |
||
23 | { |
||
24 | // Default values |
||
25 | const DEFAULT_PRIORITY = 'normal'; |
||
26 | |||
27 | // Max number of recipients for a single message |
||
28 | const MULTICAST_MAX_TOKENS = 1000; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $collapseKey; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $priority; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $contentAvailable; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $delayWhileIdle; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $timeToLive; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $restrictedPackageName; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $dryRun; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $data; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $notificationTitle; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | private $notificationBody; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $notificationIcon; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | private $notificationSound; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | private $notificationBadge; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | private $notificationTag; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | private $notificationColor; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | private $notificationClickAction; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | private $notificationTitleLocKey; |
||
114 | |||
115 | /** |
||
116 | * @var array |
||
117 | */ |
||
118 | private $notificationTitleLocArgs; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | private $notificationBodyLocKey; |
||
124 | |||
125 | /** |
||
126 | * @var array |
||
127 | */ |
||
128 | private $notificationBodyLocArgs; |
||
129 | |||
130 | /** |
||
131 | * Constructor |
||
132 | */ |
||
133 | 58 | public function __construct() |
|
134 | { |
||
135 | 58 | parent::__construct(); |
|
136 | |||
137 | 58 | $this->priority = self::DEFAULT_PRIORITY; |
|
138 | 58 | $this->data = array(); |
|
139 | 58 | $this->notificationBodyLocArgs = array(); |
|
140 | 58 | $this->notificationTitleLocArgs = array(); |
|
141 | 58 | } |
|
142 | |||
143 | /** |
||
144 | * Get full message payload. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 4 | public function getPayload() |
|
149 | { |
||
150 | // GCM base payload structure |
||
151 | 4 | $payload = array(); |
|
152 | |||
153 | // Payload for single recipient or multicast? |
||
154 | 4 | if (count($tokens = $this->getTokens()) == 1) { |
|
155 | 2 | $payload['to'] = reset($tokens); |
|
156 | 2 | } else { |
|
157 | 2 | $payload['registration_ids'] = $tokens; |
|
158 | } |
||
159 | |||
160 | // Build notification |
||
161 | 4 | if ($this->getNotificationTitle()) { |
|
162 | 1 | $payload['notification']['title'] = $this->getNotificationTitle(); |
|
163 | 1 | } |
|
164 | 4 | if ($this->getNotificationBody()) { |
|
165 | 1 | $payload['notification']['body'] = $this->getNotificationBody(); |
|
166 | 1 | } |
|
167 | 4 | if ($this->getNotificationIcon()) { |
|
168 | 1 | $payload['notification']['icon'] = $this->getNotificationIcon(); |
|
169 | 1 | } |
|
170 | 4 | if ($this->getNotificationSound()) { |
|
171 | 1 | $payload['notification']['sound'] = $this->getNotificationSound(); |
|
172 | 1 | } |
|
173 | 4 | if ($this->getNotificationTag()) { |
|
174 | 1 | $payload['notification']['tag'] = $this->getNotificationTag(); |
|
175 | 1 | } |
|
176 | 4 | if ($this->getNotificationBadge()) { |
|
177 | 1 | $payload['notification']['badge'] = $this->getNotificationBadge(); |
|
178 | 1 | } |
|
179 | 4 | if ($this->getNotificationColor()) { |
|
180 | 1 | $payload['notification']['color'] = $this->getNotificationColor(); |
|
181 | 1 | } |
|
182 | 4 | if ($this->getNotificationClickAction()) { |
|
183 | 1 | $payload['notification']['click_action'] = $this->getNotificationClickAction(); |
|
184 | 1 | } |
|
185 | 4 | if ($this->getNotificationBodyLocKey()) { |
|
186 | 1 | $payload['notification']['body_loc_key'] = $this->getNotificationBodyLocKey(); |
|
187 | 1 | } |
|
188 | 4 | if ($this->getNotificationBodyLocArgs()) { |
|
189 | 1 | $payload['notification']['body_loc_args'] = $this->getNotificationBodyLocArgs(); |
|
190 | 1 | } |
|
191 | 4 | if ($this->getNotificationTitleLocKey()) { |
|
192 | 1 | $payload['notification']['title_loc_key'] = $this->getNotificationTitleLocKey(); |
|
193 | 1 | } |
|
194 | 4 | if ($this->getNotificationTitleLocArgs()) { |
|
195 | 1 | $payload['notification']['title_loc_args'] = $this->getNotificationTitleLocArgs(); |
|
196 | 1 | } |
|
197 | |||
198 | // Build extra content |
||
199 | 4 | if ($this->getCollapseKey()) { |
|
200 | 1 | $payload['collapse_key'] = $this->getCollapseKey(); |
|
201 | 1 | } |
|
202 | 4 | if ($this->getPriority() !== self::DEFAULT_PRIORITY) { |
|
203 | 1 | $payload['priority'] = $this->getPriority(); |
|
204 | 1 | } |
|
205 | 4 | if ($this->getRestrictedPackageName()) { |
|
206 | 1 | $payload['restricted_package_name'] = $this->getRestrictedPackageName(); |
|
207 | 1 | } |
|
208 | 4 | if (!is_null($this->getContentAvailable())) { |
|
209 | 1 | $payload['content_available'] = $this->getContentAvailable(); |
|
210 | 1 | } |
|
211 | 4 | if (!is_null($this->getDelayWhileIdle())) { |
|
212 | 1 | $payload['delay_while_idle'] = $this->getDelayWhileIdle(); |
|
213 | 1 | } |
|
214 | 4 | if (!is_null($this->getDryRun())) { |
|
215 | 1 | $payload['dry_run'] = $this->getDryRun(); |
|
216 | 1 | } |
|
217 | 4 | if (!is_null($this->getTimeToLive())) { |
|
218 | 1 | $payload['time_to_live'] = $this->getTimeToLive(); |
|
219 | 1 | } |
|
220 | 4 | if ($this->getData()) { |
|
221 | 1 | $payload['data'] = $this->getData(); |
|
222 | 1 | } |
|
223 | |||
224 | // Return payload |
||
225 | 4 | return $payload; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc } |
||
230 | * |
||
231 | * @throws \RuntimeException |
||
232 | */ |
||
233 | 3 | public function setTokens(array $tokens) |
|
234 | { |
||
235 | 3 | if (count($tokens) > self::MULTICAST_MAX_TOKENS) { |
|
236 | 1 | throw new \RuntimeException(sprintf('Too many tokens in the list. %s tokens max.', self::MULTICAST_MAX_TOKENS)); |
|
237 | } |
||
238 | |||
239 | 2 | return parent::setTokens($tokens); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc } |
||
244 | * |
||
245 | * @throws \RuntimeException |
||
246 | */ |
||
247 | 5 | public function addToken($token) |
|
248 | { |
||
249 | 5 | if (count($this->tokens) + 1 > self::MULTICAST_MAX_TOKENS) { |
|
250 | 1 | throw new \RuntimeException(sprintf('Max token number reached. %s tokens max.', self::MULTICAST_MAX_TOKENS)); |
|
251 | } |
||
252 | |||
253 | 4 | return parent::addToken($token); |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get the value of Collapse Key. |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 5 | public function getCollapseKey() |
|
262 | { |
||
263 | 5 | return $this->collapseKey; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * Set the value of Collapse Key. |
||
268 | * |
||
269 | * @param string $collapseKey |
||
270 | * |
||
271 | * @return self |
||
272 | */ |
||
273 | 2 | public function setCollapseKey($collapseKey) |
|
274 | { |
||
275 | 2 | $this->collapseKey = $collapseKey; |
|
276 | |||
277 | 2 | return $this; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get the value of Priority. |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | 6 | public function getPriority() |
|
286 | { |
||
287 | 6 | return $this->priority; |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * Set the value of Priority. |
||
292 | * |
||
293 | * @param string $priority 'normal'|'hight' |
||
294 | * |
||
295 | * @return self |
||
296 | */ |
||
297 | 4 | public function setPriority($priority) |
|
298 | { |
||
299 | 4 | if (!in_array($priority, array('normal', 'high'))) { |
|
300 | 1 | throw new \RuntimeException('Bad value. Allowed priority values are "normal" or "high"'); |
|
301 | } |
||
302 | |||
303 | 3 | $this->priority = $priority; |
|
304 | |||
305 | 3 | return $this; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get the value of Content Available. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 5 | public function getContentAvailable() |
|
314 | { |
||
315 | 5 | return $this->contentAvailable; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Set the value of Content Available. |
||
320 | * |
||
321 | * @param bool $contentAvailable |
||
322 | * |
||
323 | * @return self |
||
324 | */ |
||
325 | 2 | public function setContentAvailable($contentAvailable) |
|
326 | { |
||
327 | 2 | $this->contentAvailable = !empty($contentAvailable); |
|
328 | |||
329 | 2 | return $this; |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * Get the value of Delay While Idle. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 5 | public function getDelayWhileIdle() |
|
338 | { |
||
339 | 5 | return $this->delayWhileIdle; |
|
340 | } |
||
341 | |||
342 | /** |
||
343 | * Set the value of Delay While Idle. |
||
344 | * |
||
345 | * @param bool $delayWhileIdle |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | 2 | public function setDelayWhileIdle($delayWhileIdle) |
|
350 | { |
||
351 | 2 | $this->delayWhileIdle = !empty($delayWhileIdle); |
|
352 | |||
353 | 2 | return $this; |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get the value of Time To Live. |
||
358 | * |
||
359 | * @return int |
||
360 | */ |
||
361 | 5 | public function getTimeToLive() |
|
362 | { |
||
363 | 5 | return $this->timeToLive; |
|
364 | } |
||
365 | |||
366 | /** |
||
367 | * Set the value of Time To Live. |
||
368 | * |
||
369 | * @param int $timeToLive |
||
370 | * |
||
371 | * @return self |
||
372 | */ |
||
373 | 2 | public function setTimeToLive($timeToLive) |
|
374 | { |
||
375 | 2 | $this->timeToLive = (int) $timeToLive; |
|
376 | |||
377 | 2 | return $this; |
|
378 | } |
||
379 | |||
380 | /** |
||
381 | * Get the value of Restricted Package Name. |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 5 | public function getRestrictedPackageName() |
|
386 | { |
||
387 | 5 | return $this->restrictedPackageName; |
|
388 | } |
||
389 | |||
390 | /** |
||
391 | * Set the value of Restricted Package Name. |
||
392 | * |
||
393 | * @param string $restrictedPackageName |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | 2 | public function setRestrictedPackageName($restrictedPackageName) |
|
398 | { |
||
399 | 2 | $this->restrictedPackageName = $restrictedPackageName; |
|
400 | |||
401 | 2 | return $this; |
|
402 | } |
||
403 | |||
404 | /** |
||
405 | * Get the value of Dry Run. |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | 5 | public function getDryRun() |
|
410 | { |
||
411 | 5 | return $this->dryRun; |
|
412 | } |
||
413 | |||
414 | /** |
||
415 | * Set the value of Dry Run. |
||
416 | * |
||
417 | * @param bool $dryRun |
||
418 | * |
||
419 | * @return self |
||
420 | */ |
||
421 | 2 | public function setDryRun($dryRun) |
|
422 | { |
||
423 | 2 | $this->dryRun = !empty($dryRun); |
|
424 | |||
425 | 2 | return $this; |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * Get the value of Notification Title. |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | 5 | public function getNotificationTitle() |
|
434 | { |
||
435 | 5 | return $this->notificationTitle; |
|
436 | } |
||
437 | |||
438 | /** |
||
439 | * Set the value of Notification Title. |
||
440 | * |
||
441 | * @param string $notificationTitle |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | 2 | public function setNotificationTitle($notificationTitle) |
|
446 | { |
||
447 | 2 | $this->notificationTitle = $notificationTitle; |
|
448 | |||
449 | 2 | return $this; |
|
450 | } |
||
451 | |||
452 | /** |
||
453 | * Get the value of Notification Body. |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 5 | public function getNotificationBody() |
|
458 | { |
||
459 | 5 | return $this->notificationBody; |
|
460 | } |
||
461 | |||
462 | /** |
||
463 | * Set the value of Notification Body. |
||
464 | * |
||
465 | * @param string $notificationBody |
||
466 | * |
||
467 | * @return self |
||
468 | */ |
||
469 | 2 | public function setNotificationBody($notificationBody) |
|
470 | { |
||
471 | 2 | $this->notificationBody = $notificationBody; |
|
472 | |||
473 | 2 | return $this; |
|
474 | } |
||
475 | |||
476 | /** |
||
477 | * Get the value of Notification Icon. |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | 5 | public function getNotificationIcon() |
|
482 | { |
||
483 | 5 | return $this->notificationIcon; |
|
484 | } |
||
485 | |||
486 | /** |
||
487 | * Set the value of Notification Icon. |
||
488 | * |
||
489 | * @param string $notificationIcon |
||
490 | * |
||
491 | * @return self |
||
492 | */ |
||
493 | 2 | public function setNotificationIcon($notificationIcon) |
|
494 | { |
||
495 | 2 | $this->notificationIcon = $notificationIcon; |
|
496 | |||
497 | 2 | return $this; |
|
498 | } |
||
499 | |||
500 | /** |
||
501 | * Get the value of Notification Sound. |
||
502 | * |
||
503 | * @return string |
||
504 | */ |
||
505 | 5 | public function getNotificationSound() |
|
506 | { |
||
507 | 5 | return $this->notificationSound; |
|
508 | } |
||
509 | |||
510 | /** |
||
511 | * Set the value of Notification Sound. |
||
512 | * |
||
513 | * @param string $notificationSound |
||
514 | * |
||
515 | * @return self |
||
516 | */ |
||
517 | 2 | public function setNotificationSound($notificationSound) |
|
518 | { |
||
519 | 2 | $this->notificationSound = $notificationSound; |
|
520 | |||
521 | 2 | return $this; |
|
522 | } |
||
523 | |||
524 | /** |
||
525 | * Get the value of Notification Badge. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 5 | public function getNotificationBadge() |
|
530 | { |
||
531 | 5 | return $this->notificationBadge; |
|
532 | } |
||
533 | |||
534 | /** |
||
535 | * Set the value of Notification Badge. |
||
536 | * |
||
537 | * @param string $notificationBadge |
||
538 | * |
||
539 | * @return self |
||
540 | */ |
||
541 | 2 | public function setNotificationBadge($notificationBadge) |
|
542 | { |
||
543 | 2 | $this->notificationBadge = $notificationBadge; |
|
544 | |||
545 | 2 | return $this; |
|
546 | } |
||
547 | |||
548 | /** |
||
549 | * Get the value of Notification Tag. |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | 5 | public function getNotificationTag() |
|
554 | { |
||
555 | 5 | return $this->notificationTag; |
|
556 | } |
||
557 | |||
558 | /** |
||
559 | * Set the value of Notification Tag. |
||
560 | * |
||
561 | * @param string $notificationTag |
||
562 | * |
||
563 | * @return self |
||
564 | */ |
||
565 | 2 | public function setNotificationTag($notificationTag) |
|
566 | { |
||
567 | 2 | $this->notificationTag = $notificationTag; |
|
568 | |||
569 | 2 | return $this; |
|
570 | } |
||
571 | |||
572 | /** |
||
573 | * Get the value of Notification Color. |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | 5 | public function getNotificationColor() |
|
578 | { |
||
579 | 5 | return $this->notificationColor; |
|
580 | } |
||
581 | |||
582 | /** |
||
583 | * Set the value of Notification Color. |
||
584 | * |
||
585 | * @param string $notificationColor |
||
586 | * |
||
587 | * @return self |
||
588 | */ |
||
589 | 2 | public function setNotificationColor($notificationColor) |
|
590 | { |
||
591 | 2 | $this->notificationColor = $notificationColor; |
|
592 | |||
593 | 2 | return $this; |
|
594 | } |
||
595 | |||
596 | /** |
||
597 | * Get the value of Notification Click Action. |
||
598 | * |
||
599 | * @return string |
||
600 | */ |
||
601 | 5 | public function getNotificationClickAction() |
|
602 | { |
||
603 | 5 | return $this->notificationClickAction; |
|
604 | } |
||
605 | |||
606 | /** |
||
607 | * Set the value of Notification Click Action. |
||
608 | * |
||
609 | * @param string $notificationClickAction |
||
610 | * |
||
611 | * @return self |
||
612 | */ |
||
613 | 2 | public function setNotificationClickAction($notificationClickAction) |
|
614 | { |
||
615 | 2 | $this->notificationClickAction = $notificationClickAction; |
|
616 | |||
617 | 2 | return $this; |
|
618 | } |
||
619 | |||
620 | /** |
||
621 | * Get the value of Notification Body Loc Key. |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | 5 | public function getNotificationBodyLocKey() |
|
626 | { |
||
627 | 5 | return $this->notificationBodyLocKey; |
|
628 | } |
||
629 | |||
630 | /** |
||
631 | * Set the value of Notification Body Loc Key. |
||
632 | * |
||
633 | * @param string $notificationBodyLocKey |
||
634 | * |
||
635 | * @return self |
||
636 | */ |
||
637 | 2 | public function setNotificationBodyLocKey($notificationBodyLocKey) |
|
638 | { |
||
639 | 2 | $this->notificationBodyLocKey = $notificationBodyLocKey; |
|
640 | |||
641 | 2 | return $this; |
|
642 | } |
||
643 | |||
644 | /** |
||
645 | * Get the value of Notification Body Loc Args. |
||
646 | * |
||
647 | * @return array |
||
648 | */ |
||
649 | 5 | public function getNotificationBodyLocArgs() |
|
650 | { |
||
651 | 5 | return $this->notificationBodyLocArgs; |
|
652 | } |
||
653 | |||
654 | /** |
||
655 | * Set the value of Notification Body Loc Args. |
||
656 | * |
||
657 | * @param array $notificationBodyLocArgs |
||
658 | * |
||
659 | * @return self |
||
660 | */ |
||
661 | 2 | public function setNotificationBodyLocArgs(array $notificationBodyLocArgs) |
|
662 | { |
||
663 | 2 | $this->notificationBodyLocArgs = $notificationBodyLocArgs; |
|
664 | |||
665 | 2 | return $this; |
|
666 | } |
||
667 | |||
668 | /** |
||
669 | * Get the value of Notification Title Loc Key. |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | 5 | public function getNotificationTitleLocKey() |
|
674 | { |
||
675 | 5 | return $this->notificationTitleLocKey; |
|
676 | } |
||
677 | |||
678 | /** |
||
679 | * Set the value of Notification Title Loc Key. |
||
680 | * |
||
681 | * @param string $notificationTitleLocKey |
||
682 | * |
||
683 | * @return self |
||
684 | */ |
||
685 | 2 | public function setNotificationTitleLocKey($notificationTitleLocKey) |
|
686 | { |
||
687 | 2 | $this->notificationTitleLocKey = $notificationTitleLocKey; |
|
688 | |||
689 | 2 | return $this; |
|
690 | } |
||
691 | |||
692 | /** |
||
693 | * Get the value of Notification Title Loc Args. |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | 5 | public function getNotificationTitleLocArgs() |
|
698 | { |
||
699 | 5 | return $this->notificationTitleLocArgs; |
|
700 | } |
||
701 | |||
702 | /** |
||
703 | * Set the value of Notification Title Loc Args. |
||
704 | * |
||
705 | * @param array $notificationTitleLocArgs |
||
706 | * |
||
707 | * @return self |
||
708 | */ |
||
709 | 2 | public function setNotificationTitleLocArgs(array $notificationTitleLocArgs) |
|
710 | { |
||
711 | 2 | $this->notificationTitleLocArgs = $notificationTitleLocArgs; |
|
712 | |||
713 | 2 | return $this; |
|
714 | } |
||
715 | |||
716 | /** |
||
717 | * Get the value of Data. |
||
718 | * |
||
719 | * @return array |
||
720 | */ |
||
721 | 6 | public function getData() |
|
722 | { |
||
723 | 6 | return $this->data; |
|
724 | } |
||
725 | |||
726 | /** |
||
727 | * Set the value of Data. |
||
728 | * |
||
729 | * @param array $data |
||
730 | * |
||
731 | * @return self |
||
732 | */ |
||
733 | 7 | public function setData(array $data) |
|
734 | { |
||
735 | $reservedDataKeys = array( |
||
736 | 7 | 'from', |
|
737 | 7 | 'notification', |
|
738 | 7 | 'to', |
|
739 | 7 | 'registration_ids', |
|
740 | 7 | 'collapse_key', |
|
741 | 7 | 'priority', |
|
742 | 7 | 'restricted_package_name', |
|
743 | 7 | 'content_available', |
|
744 | 7 | 'delay_while_idle', |
|
745 | 7 | 'dry_run', |
|
746 | 7 | 'time_to_live', |
|
747 | 7 | 'data', |
|
748 | 7 | ); |
|
749 | |||
750 | 7 | foreach ($data as $key => $value) { |
|
751 | |||
752 | 7 | if (!is_string($key)) { |
|
753 | 1 | throw new \InvalidArgumentException('Data keys must be of type string in order to convert data in a valid JSON Object.'); |
|
754 | } |
||
755 | |||
756 | 6 | if (in_array($key, $reservedDataKeys) |
|
757 | 5 | || strpos($key, 'google') === 0 |
|
758 | 5 | || strpos($key, 'gcm') === 0 |
|
759 | 6 | ) { |
|
760 | 3 | throw new \InvalidArgumentException(sprintf( |
|
761 | 3 | 'The key "%s" is reserved or not recommended. Do not use it as data key.', |
|
762 | $key |
||
763 | 3 | )); |
|
764 | } |
||
765 | |||
766 | 3 | } |
|
767 | |||
768 | 3 | $this->data = $data; |
|
769 | |||
770 | 3 | return $this; |
|
771 | } |
||
772 | |||
773 | /** |
||
774 | * Set a key/value pair in the data array. |
||
775 | * |
||
776 | * @param string $key |
||
777 | * @param mixed $value |
||
778 | * |
||
779 | * @return self |
||
780 | */ |
||
781 | 2 | View Code Duplication | public function addData($key, $value) |
0 ignored issues
–
show
|
|||
782 | { |
||
783 | 2 | if (!is_string($key)) { |
|
784 | 1 | throw new \InvalidArgumentException('Data keys must be of type string in order to convert data in a valid JSON Object.'); |
|
785 | } |
||
786 | |||
787 | 1 | $data = $this->getData(); |
|
788 | |||
789 | 1 | $data[$key] = $value; |
|
790 | |||
791 | 1 | return $this->setData($data); |
|
792 | } |
||
793 | } |
||
794 |
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.