Completed
Push — master ( c84001...5d8c0b )
by Oliver
9s
created

GcmMessage::setNotificationBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
        $payload = array(
152 4
            'notification' => array(),
153 4
        );
154
155
        // Payload for single recipient or multicast?
156 4
        if (count($tokens = $this->getTokens()) == 1) {
157 2
            $payload['to'] = reset($tokens);
158 2
        } else {
159 2
            $payload['registration_ids'] = $tokens;
160
        }
161
162
        // Build notification
163 4
        if ($this->getNotificationTitle()) {
164 1
            $payload['notification']['title'] = $this->getNotificationTitle();
165 1
        }
166 4
        if ($this->getNotificationBody()) {
167 1
            $payload['notification']['body'] = $this->getNotificationBody();
168 1
        }
169 4
        if ($this->getNotificationIcon()) {
170 1
            $payload['notification']['icon'] = $this->getNotificationIcon();
171 1
        }
172 4
        if ($this->getNotificationSound()) {
173 1
            $payload['notification']['sound'] = $this->getNotificationSound();
174 1
        }
175 4
        if ($this->getNotificationTag()) {
176 1
            $payload['notification']['tag'] = $this->getNotificationTag();
177 1
        }
178 4
        if ($this->getNotificationBadge()) {
179 1
            $payload['notification']['badge'] = $this->getNotificationBadge();
180 1
        }
181 4
        if ($this->getNotificationColor()) {
182 1
            $payload['notification']['color'] = $this->getNotificationColor();
183 1
        }
184 4
        if ($this->getNotificationClickAction()) {
185 1
            $payload['notification']['click_action'] = $this->getNotificationClickAction();
186 1
        }
187 4
        if ($this->getNotificationBodyLocKey()) {
188 1
            $payload['notification']['body_loc_key'] = $this->getNotificationBodyLocKey();
189 1
        }
190 4
        if ($this->getNotificationBodyLocArgs()) {
191 1
            $payload['notification']['body_loc_args'] = $this->getNotificationBodyLocArgs();
192 1
        }
193 4
        if ($this->getNotificationTitleLocKey()) {
194 1
            $payload['notification']['title_loc_key'] = $this->getNotificationTitleLocKey();
195 1
        }
196 4
        if ($this->getNotificationTitleLocArgs()) {
197 1
            $payload['notification']['title_loc_args'] = $this->getNotificationTitleLocArgs();
198 1
        }
199
200
        // Build extra content
201 4
        if ($this->getCollapseKey()) {
202 1
            $payload['collapse_key'] = $this->getCollapseKey();
203 1
        }
204 4
        if ($this->getPriority() !== self::DEFAULT_PRIORITY) {
205 1
            $payload['priority'] = $this->getPriority();
206 1
        }
207 4
        if ($this->getRestrictedPackageName()) {
208 1
            $payload['restricted_package_name'] = $this->getRestrictedPackageName();
209 1
        }
210 4
        if (!is_null($this->getContentAvailable())) {
211 1
            $payload['content_available'] = $this->getContentAvailable();
212 1
        }
213 4
        if (!is_null($this->getDelayWhileIdle())) {
214 1
            $payload['delay_while_idle'] = $this->getDelayWhileIdle();
215 1
        }
216 4
        if (!is_null($this->getDryRun())) {
217 1
            $payload['dry_run'] = $this->getDryRun();
218 1
        }
219 4
        if (!is_null($this->getTimeToLive())) {
220 1
            $payload['time_to_live'] = $this->getTimeToLive();
221 1
        }
222 4
        if ($this->getData()) {
223 1
            $payload['data'] = $this->getData();
224 1
        }
225
226
        // Return payload
227 4
        return $payload;
228
    }
229
230
    /**
231
     * {@inheritdoc }
232
     *
233
     * @throws \RuntimeException
234
     */
235 3
    public function setTokens(array $tokens)
236
    {
237 3
        if (count($tokens) > self::MULTICAST_MAX_TOKENS) {
238 1
            throw new \RuntimeException(sprintf('Too many tokens in the list. %s tokens max.', self::MULTICAST_MAX_TOKENS));
239
        }
240
241 2
        return parent::setTokens($tokens);
242
    }
243
244
    /**
245
     * {@inheritdoc }
246
     *
247
     * @throws \RuntimeException
248
     */
249 5
    public function addToken($token)
250
    {
251 5
        if (count($this->tokens) + 1 > self::MULTICAST_MAX_TOKENS) {
252 1
            throw new \RuntimeException(sprintf('Max token number reached. %s tokens max.', self::MULTICAST_MAX_TOKENS));
253
        }
254
255 4
        return parent::addToken($token);
256
    }
257
258
    /**
259
     * Get the value of Collapse Key.
260
     *
261
     * @return string
262
     */
263 5
    public function getCollapseKey()
264
    {
265 5
        return $this->collapseKey;
266
    }
267
268
    /**
269
     * Set the value of Collapse Key.
270
     *
271
     * @param string $collapseKey
272
     *
273
     * @return self
274
     */
275 2
    public function setCollapseKey($collapseKey)
276
    {
277 2
        $this->collapseKey = $collapseKey;
278
279 2
        return $this;
280
    }
281
282
    /**
283
     * Get the value of Priority.
284
     *
285
     * @return string
286
     */
287 6
    public function getPriority()
288
    {
289 6
        return $this->priority;
290
    }
291
292
    /**
293
     * Set the value of Priority.
294
     *
295
     * @param string $priority 'normal'|'hight'
296
     *
297
     * @return self
298
     */
299 4
    public function setPriority($priority)
300
    {
301 4
        if (!in_array($priority, array('normal', 'high'))) {
302 1
            throw new \RuntimeException('Bad value. Allowed priority values are "normal" or "high"');
303
        }
304
305 3
        $this->priority = $priority;
306
307 3
        return $this;
308
    }
309
310
    /**
311
     * Get the value of Content Available.
312
     *
313
     * @return bool
314
     */
315 5
    public function getContentAvailable()
316
    {
317 5
        return $this->contentAvailable;
318
    }
319
320
    /**
321
     * Set the value of Content Available.
322
     *
323
     * @param bool $contentAvailable
324
     *
325
     * @return self
326
     */
327 2
    public function setContentAvailable($contentAvailable)
328
    {
329 2
        $this->contentAvailable = !empty($contentAvailable);
330
331 2
        return $this;
332
    }
333
334
    /**
335
     * Get the value of Delay While Idle.
336
     *
337
     * @return bool
338
     */
339 5
    public function getDelayWhileIdle()
340
    {
341 5
        return $this->delayWhileIdle;
342
    }
343
344
    /**
345
     * Set the value of Delay While Idle.
346
     *
347
     * @param bool $delayWhileIdle
348
     *
349
     * @return self
350
     */
351 2
    public function setDelayWhileIdle($delayWhileIdle)
352
    {
353 2
        $this->delayWhileIdle = !empty($delayWhileIdle);
354
355 2
        return $this;
356
    }
357
358
    /**
359
     * Get the value of Time To Live.
360
     *
361
     * @return int
362
     */
363 5
    public function getTimeToLive()
364
    {
365 5
        return $this->timeToLive;
366
    }
367
368
    /**
369
     * Set the value of Time To Live.
370
     *
371
     * @param int $timeToLive
372
     *
373
     * @return self
374
     */
375 2
    public function setTimeToLive($timeToLive)
376
    {
377 2
        $this->timeToLive = (int) $timeToLive;
378
379 2
        return $this;
380
    }
381
382
    /**
383
     * Get the value of Restricted Package Name.
384
     *
385
     * @return string
386
     */
387 5
    public function getRestrictedPackageName()
388
    {
389 5
        return $this->restrictedPackageName;
390
    }
391
392
    /**
393
     * Set the value of Restricted Package Name.
394
     *
395
     * @param string $restrictedPackageName
396
     *
397
     * @return self
398
     */
399 2
    public function setRestrictedPackageName($restrictedPackageName)
400
    {
401 2
        $this->restrictedPackageName = $restrictedPackageName;
402
403 2
        return $this;
404
    }
405
406
    /**
407
     * Get the value of Dry Run.
408
     *
409
     * @return bool
410
     */
411 5
    public function getDryRun()
412
    {
413 5
        return $this->dryRun;
414
    }
415
416
    /**
417
     * Set the value of Dry Run.
418
     *
419
     * @param bool $dryRun
420
     *
421
     * @return self
422
     */
423 2
    public function setDryRun($dryRun)
424
    {
425 2
        $this->dryRun = !empty($dryRun);
426
427 2
        return $this;
428
    }
429
430
    /**
431
     * Get the value of Notification Title.
432
     *
433
     * @return string
434
     */
435 5
    public function getNotificationTitle()
436
    {
437 5
        return $this->notificationTitle;
438
    }
439
440
    /**
441
     * Set the value of Notification Title.
442
     *
443
     * @param string $notificationTitle
444
     *
445
     * @return self
446
     */
447 2
    public function setNotificationTitle($notificationTitle)
448
    {
449 2
        $this->notificationTitle = $notificationTitle;
450
451 2
        return $this;
452
    }
453
454
    /**
455
     * Get the value of Notification Body.
456
     *
457
     * @return string
458
     */
459 5
    public function getNotificationBody()
460
    {
461 5
        return $this->notificationBody;
462
    }
463
464
    /**
465
     * Set the value of Notification Body.
466
     *
467
     * @param string $notificationBody
468
     *
469
     * @return self
470
     */
471 2
    public function setNotificationBody($notificationBody)
472
    {
473 2
        $this->notificationBody = $notificationBody;
474
475 2
        return $this;
476
    }
477
478
    /**
479
     * Get the value of Notification Icon.
480
     *
481
     * @return string
482
     */
483 5
    public function getNotificationIcon()
484
    {
485 5
        return $this->notificationIcon;
486
    }
487
488
    /**
489
     * Set the value of Notification Icon.
490
     *
491
     * @param string $notificationIcon
492
     *
493
     * @return self
494
     */
495 2
    public function setNotificationIcon($notificationIcon)
496
    {
497 2
        $this->notificationIcon = $notificationIcon;
498
499 2
        return $this;
500
    }
501
502
    /**
503
     * Get the value of Notification Sound.
504
     *
505
     * @return string
506
     */
507 5
    public function getNotificationSound()
508
    {
509 5
        return $this->notificationSound;
510
    }
511
512
    /**
513
     * Set the value of Notification Sound.
514
     *
515
     * @param string $notificationSound
516
     *
517
     * @return self
518
     */
519 2
    public function setNotificationSound($notificationSound)
520
    {
521 2
        $this->notificationSound = $notificationSound;
522
523 2
        return $this;
524
    }
525
526
    /**
527
     * Get the value of Notification Badge.
528
     *
529
     * @return string
530
     */
531 5
    public function getNotificationBadge()
532
    {
533 5
        return $this->notificationBadge;
534
    }
535
536
    /**
537
     * Set the value of Notification Badge.
538
     *
539
     * @param string $notificationBadge
540
     *
541
     * @return self
542
     */
543 2
    public function setNotificationBadge($notificationBadge)
544
    {
545 2
        $this->notificationBadge = $notificationBadge;
546
547 2
        return $this;
548
    }
549
550
    /**
551
     * Get the value of Notification Tag.
552
     *
553
     * @return string
554
     */
555 5
    public function getNotificationTag()
556
    {
557 5
        return $this->notificationTag;
558
    }
559
560
    /**
561
     * Set the value of Notification Tag.
562
     *
563
     * @param string $notificationTag
564
     *
565
     * @return self
566
     */
567 2
    public function setNotificationTag($notificationTag)
568
    {
569 2
        $this->notificationTag = $notificationTag;
570
571 2
        return $this;
572
    }
573
574
    /**
575
     * Get the value of Notification Color.
576
     *
577
     * @return string
578
     */
579 5
    public function getNotificationColor()
580
    {
581 5
        return $this->notificationColor;
582
    }
583
584
    /**
585
     * Set the value of Notification Color.
586
     *
587
     * @param string $notificationColor
588
     *
589
     * @return self
590
     */
591 2
    public function setNotificationColor($notificationColor)
592
    {
593 2
        $this->notificationColor = $notificationColor;
594
595 2
        return $this;
596
    }
597
598
    /**
599
     * Get the value of Notification Click Action.
600
     *
601
     * @return string
602
     */
603 5
    public function getNotificationClickAction()
604
    {
605 5
        return $this->notificationClickAction;
606
    }
607
608
    /**
609
     * Set the value of Notification Click Action.
610
     *
611
     * @param string $notificationClickAction
612
     *
613
     * @return self
614
     */
615 2
    public function setNotificationClickAction($notificationClickAction)
616
    {
617 2
        $this->notificationClickAction = $notificationClickAction;
618
619 2
        return $this;
620
    }
621
622
    /**
623
     * Get the value of Notification Body Loc Key.
624
     *
625
     * @return string
626
     */
627 5
    public function getNotificationBodyLocKey()
628
    {
629 5
        return $this->notificationBodyLocKey;
630
    }
631
632
    /**
633
     * Set the value of Notification Body Loc Key.
634
     *
635
     * @param string $notificationBodyLocKey
636
     *
637
     * @return self
638
     */
639 2
    public function setNotificationBodyLocKey($notificationBodyLocKey)
640
    {
641 2
        $this->notificationBodyLocKey = $notificationBodyLocKey;
642
643 2
        return $this;
644
    }
645
646
    /**
647
     * Get the value of Notification Body Loc Args.
648
     *
649
     * @return array
650
     */
651 5
    public function getNotificationBodyLocArgs()
652
    {
653 5
        return $this->notificationBodyLocArgs;
654
    }
655
656
    /**
657
     * Set the value of Notification Body Loc Args.
658
     *
659
     * @param array $notificationBodyLocArgs
660
     *
661
     * @return self
662
     */
663 2
    public function setNotificationBodyLocArgs(array $notificationBodyLocArgs)
664
    {
665 2
        $this->notificationBodyLocArgs = $notificationBodyLocArgs;
666
667 2
        return $this;
668
    }
669
670
    /**
671
     * Get the value of Notification Title Loc Key.
672
     *
673
     * @return string
674
     */
675 5
    public function getNotificationTitleLocKey()
676
    {
677 5
        return $this->notificationTitleLocKey;
678
    }
679
680
    /**
681
     * Set the value of Notification Title Loc Key.
682
     *
683
     * @param string $notificationTitleLocKey
684
     *
685
     * @return self
686
     */
687 2
    public function setNotificationTitleLocKey($notificationTitleLocKey)
688
    {
689 2
        $this->notificationTitleLocKey = $notificationTitleLocKey;
690
691 2
        return $this;
692
    }
693
694
    /**
695
     * Get the value of Notification Title Loc Args.
696
     *
697
     * @return array
698
     */
699 5
    public function getNotificationTitleLocArgs()
700
    {
701 5
        return $this->notificationTitleLocArgs;
702
    }
703
704
    /**
705
     * Set the value of Notification Title Loc Args.
706
     *
707
     * @param array $notificationTitleLocArgs
708
     *
709
     * @return self
710
     */
711 2
    public function setNotificationTitleLocArgs(array $notificationTitleLocArgs)
712
    {
713 2
        $this->notificationTitleLocArgs = $notificationTitleLocArgs;
714
715 2
        return $this;
716
    }
717
718
    /**
719
     * Get the value of Data.
720
     *
721
     * @return array
722
     */
723 6
    public function getData()
724
    {
725 6
        return $this->data;
726
    }
727
728
    /**
729
     * Set the value of Data.
730
     *
731
     * @param array $data
732
     *
733
     * @return self
734
     */
735 7
    public function setData(array $data)
736
    {
737
        $reservedDataKeys = array(
738 7
            'from',
739 7
            'notification',
740 7
            'to',
741 7
            'registration_ids',
742 7
            'collapse_key',
743 7
            'priority',
744 7
            'restricted_package_name',
745 7
            'content_available',
746 7
            'delay_while_idle',
747 7
            'dry_run',
748 7
            'time_to_live',
749 7
            'data',
750 7
        );
751
752 7
        foreach ($data as $key => $value) {
753
754 7
            if (!is_string($key)) {
755 1
                throw new \InvalidArgumentException('Data keys must be of type string in order to convert data in a valid JSON Object.');
756
            }
757
758 6
            if (in_array($key, $reservedDataKeys)
759 5
                || strpos($key, 'google') === 0
760 5
                || strpos($key, 'gcm') === 0
761 6
            ) {
762 3
                throw new \InvalidArgumentException(sprintf(
763 3
                    'The key "%s" is reserved or not recommended. Do not use it as data key.',
764
                    $key
765 3
                ));
766
            }
767
768 3
        }
769
770 3
        $this->data = $data;
771
772 3
        return $this;
773
    }
774
775
    /**
776
     * Set a key/value pair in the data array.
777
     *
778
     * @param string $key
779
     * @param mixed $value
780
     *
781
     * @return self
782
     */
783 2 View Code Duplication
    public function addData($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
784
    {
785 2
        if (!is_string($key)) {
786 1
            throw new \InvalidArgumentException('Data keys must be of type string in order to convert data in a valid JSON Object.');
787
        }
788
789 1
        $data = $this->getData();
790
791 1
        $data[$key] = $value;
792
793 1
        return $this->setData($data);
794
    }
795
}
796