Passed
Branch version-1 (58750c)
by Alex
02:31
created

PushMessage::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AlexLisenkov\LaravelWebPush;
4
5
use AlexLisenkov\LaravelWebPush\Contracts\MessageActionContract;
6
use AlexLisenkov\LaravelWebPush\Contracts\PushMessageContract;
7
use AlexLisenkov\LaravelWebPush\Contracts\PushSubscriptionContract;
8
use AlexLisenkov\LaravelWebPush\Contracts\WebPushContract;
9
use GuzzleHttp\Promise\PromiseInterface;
10
use Illuminate\Support\Facades\App;
11
12
class PushMessage implements PushMessageContract
13
{
14
    /**
15
     * JSON encoding options
16
     */
17
    public const DEFAULT_ENCODING_OPTIONS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
18
19
    /**
20
     * Available notification directions
21
     */
22
    public const NOTIFICATION_DIRECTIONS = ['auto', 'ltr', 'rtl'];
23
24
    /**
25
     * The title that must be shown within the notification
26
     *
27
     * @var string
28
     */
29
    protected $title = '';
30
31
    /**
32
     * An array of actions to display in the notification. The members of the array should be an object literal. It may
33
     * contain the following values:
34
     *
35
     * @var MessageActionContract[]
36
     */
37
    protected $actions;
38
39
    /**
40
     * A badge resource is an icon representing the web application, or the category of the notification if the web
41
     * application sends a wide variety of notifications. It may be used to represent the notification when there is
42
     * not enough space to display the notification itself. It may also be displayed inside the notification, but then
43
     * it should have less visual priority than the image resource and icon resource.
44
     *
45
     * @string|null
46
     */
47
    protected $badge;
48
49
    /**
50
     * A string representing an extra content to display within the notification.
51
     *
52
     * @var string
53
     */
54
    protected $body = '';
55
56
    /**
57
     * Arbitrary data that you want to be associated with the notification. This can be of any data type.
58
     */
59
    protected $data;
60
61
    /**
62
     * The direction of the notification; it can be auto, ltr or rtl
63
     *
64
     * @var string
65
     */
66
    protected $dir = 'auto';
67
68
    /**
69
     * The URL of an image to be used as an icon by the notification.
70
     *
71
     * @var null|string
72
     */
73
    protected $icon;
74
75
    /**
76
     * An image resource is a picture shown as part of the content of the notification, and should be displayed with
77
     * higher visual priority than the icon resource and badge resource, though it may be displayed in fewer
78
     * circumstances.
79
     *
80
     * @var string|null
81
     */
82
    protected $image;
83
84
    /**
85
     * Specify the lang used within the notification. This string must be a valid BCP 47 language tag.
86
     * https://tools.ietf.org/html/bcp47
87
     *
88
     * @var string|null
89
     */
90
    protected $lang;
91
92
    /**
93
     * When set indicates that the end user should be alerted after the show steps have run with a new notification
94
     * that has the same tag as an existing notification.
95
     *
96
     * @bool
97
     */
98
    protected $renotify = false;
99
100
    /**
101
     * When set, indicates that on devices with a sufficiently large screen, the notification should remain readily
102
     * available until the user activates or dismisses the notification.
103
     *
104
     * @bool
105
     */
106
    protected $require_interaction = false;
107
108
    /**
109
     * An ID for a given notification that allows you to find, replace, or remove the notification using a script if
110
     * necessary.
111
     *
112
     * @var string|null
113
     */
114
    protected $tag;
115
116
    /**
117
     * Timestamp which is a DOMTimeStamp representing the time, in milliseconds since 00:00:00 UTC on 1 January 1970,
118
     * of the event for which the notification was created.
119
     * https://heycam.github.io/webidl/#DOMTimeStamp
120
     *
121
     * @var int|null
122
     */
123
    protected $timestamp;
124
125
    /**
126
     * When set indicates that no sounds or vibrations should be made.
127
     *
128
     * @var bool|null
129
     */
130
    protected $silent;
131
132
    /**
133
     * Topics are strings that can be used to replace a pending messages with a new message if they have matching topic
134
     * names. This is useful in scenarios where multiple messages are sent while a device is offline, and you really
135
     * only want a user to see the latest message when the device is turned on.
136
     *
137
     * @var string
138
     */
139
    protected $topic;
140
141
    /**
142
     * Urgency indicates to the push service how important a message is to the user. This can be used by the push
143
     * service to help conserve the battery life of a user's device by only waking up for important messages when
144
     * battery is low. It can be: very-low, low, normal or high
145
     *
146
     * @var string|null
147
     */
148
    protected $urgency;
149
150
    /**
151
     * https://w3c.github.io/vibration/#idl-def-vibratepattern
152
     * A vibration pattern to run with the display of the notification.
153
     * A vibration pattern can be an array with as few as one member.
154
     * The values are times in milliseconds where the even indices (0, 2, 4, etc.) indicate how long to vibrate and the
155
     * odd indices indicate how long to pause.
156
     * For example, [300, 100, 400] would vibrate 300ms, pause 100ms, then vibrate 400ms.
157
     *
158
     * @var array|null
159
     */
160
    protected $vibrate = [0, 200, 1000];
161
162
    /**
163
     * @param PushSubscriptionContract $push_subscription
164
     *
165
     * @return PromiseInterface
166
     */
167 1
    public function sendTo(PushSubscriptionContract $push_subscription): PromiseInterface
168
    {
169
        /** @var WebPushContract $web_push */
170 1
        $web_push = App::make(WebPushContract::class);
171
172 1
        return $web_push->sendMessage($this, $push_subscription);
173
    }
174
175
    /**
176
     * @return string
177
     */
178 1
    public function __toString(): string
179
    {
180 1
        if ($string = $this->toJson()) {
181 1
            return (string) $string;
182
        }
183
184
        return '';
185
    }
186
187
    /**
188
     * @param int $options
189
     *
190
     * @return false|string
191
     */
192 3
    public function toJson($options = self::DEFAULT_ENCODING_OPTIONS)
193
    {
194 3
        return json_encode($this->toArray(), $options);
195
    }
196
197
    /**
198
     * @return array
199
     */
200 9
    public function toArray(): array
201
    {
202 9
        return array_filter([
203 9
            'title' => $this->getTitle(),
204 9
            'options' => array_filter([
205 9
                'actions' => $this->mapActionsToArray(),
206 9
                'badge' => $this->getBadge(),
207 9
                'body' => $this->getBody(),
208 9
                'data' => $this->getData(),
209 9
                'dir' => $this->getDir(),
210 9
                'icon' => $this->getIcon(),
211 9
                'image' => $this->getImage(),
212 9
                'lang' => $this->getLang(),
213 9
                'renotify' => $this->getRenotify(),
214 9
                'requireInteraction' => $this->getRequireInteraction(),
215 9
                'silent' => $this->isSilent(),
216 9
                'tag' => $this->getTag(),
217 9
                'timestamp' => $this->getTimestamp(),
218 9
                'vibrate' => !$this->isSilent() ? $this->getVibrate() : null,
219
            ]),
220
        ]);
221
    }
222
223
    /**
224
     * Get Title
225
     *
226
     * @return string
227
     */
228 10
    public function getTitle(): string
229
    {
230 10
        return $this->title;
231
    }
232
233
    /**
234
     * Set Title
235
     *
236
     * @param string $title
237
     *
238
     * @return PushMessageContract
239
     */
240 5
    public function setTitle(string $title): PushMessageContract
241
    {
242 5
        $this->title = $title;
243
244 5
        return $this;
245
    }
246
247
    /**
248
     * @return array|null
249
     */
250 9
    private function mapActionsToArray(): ?array
251
    {
252 9
        if ($this->getActions() === null) {
0 ignored issues
show
introduced by
The condition $this->getActions() === null is always false.
Loading history...
253 7
            return null;
254
        }
255
256 2
        $actions = $this->getActions();
257
258
        return array_map(function (MessageActionContract $action) {
259 1
            return $action->toArray();
260 2
        }, $actions);
261
    }
262
263
    /**
264
     * Get Actions
265
     *
266
     * @return MessageActionContract[]|null
267
     */
268 10
    public function getActions(): ?array
269
    {
270 10
        return $this->actions;
271
    }
272
273
    /**
274
     * Set Actions
275
     *
276
     * @param MessageActionContract[] $actions
277
     *
278
     * @return PushMessage
279
     */
280 5
    public function setActions(?array $actions): PushMessage
281
    {
282 5
        $this->assertActionsImplementContract($actions ?? []);
283
284 4
        $this->actions = $actions;
285
286 4
        return $this;
287
    }
288
289 5
    private function assertActionsImplementContract($actions): void
290
    {
291 5
        foreach ($actions as $action) {
292 2
            if (!$action instanceof MessageActionContract) {
293 2
                throw new \InvalidArgumentException(get_class($action) . ' must implement ' . MessageActionContract::class);
294
            }
295
        }
296 4
    }
297
298
    /**
299
     * Get Badge
300
     *
301
     * @return string|null
302
     */
303 10
    public function getBadge(): ?string
304
    {
305 10
        return $this->badge;
306
    }
307
308
    /**
309
     * Set Badge
310
     *
311
     * @param string $badge
312
     *
313
     * @return PushMessage
314
     */
315 1
    public function setBadge(string $badge): PushMessage
316
    {
317 1
        $this->badge = $badge;
318
319 1
        return $this;
320
    }
321
322
    /**
323
     * Get Body
324
     *
325
     * @return string
326
     */
327 10
    public function getBody(): string
328
    {
329 10
        return $this->body;
330
    }
331
332
    /**
333
     * Set Body
334
     *
335
     * @param string $body
336
     *
337
     * @return PushMessageContract
338
     */
339 5
    public function setBody(string $body): PushMessageContract
340
    {
341 5
        $this->body = $body;
342
343 5
        return $this;
344
    }
345
346
    /**
347
     * Get Data
348
     *
349
     * @return mixed
350
     */
351 10
    public function getData()
352
    {
353 10
        return $this->data;
354
    }
355
356
    /**
357
     * Set Data
358
     *
359
     * @param mixed $data
360
     *
361
     * @return PushMessage
362
     */
363 1
    public function setData($data): PushMessage
364
    {
365 1
        $this->data = $data;
366
367 1
        return $this;
368
    }
369
370
    /**
371
     * Get Dir
372
     *
373
     * @return string
374
     */
375 13
    public function getDir(): string
376
    {
377 13
        return $this->dir;
378
    }
379
380
    /**
381
     * Set Dir
382
     *
383
     * @param string $dir
384
     *
385
     * @return PushMessage
386
     */
387 4
    public function setDir(string $dir): PushMessage
388
    {
389 4
        if (!in_array($dir, self::NOTIFICATION_DIRECTIONS, true)) {
390 1
            throw new \InvalidArgumentException('Direction must be one of ' . implode(', ',
391 1
                    self::NOTIFICATION_DIRECTIONS));
392
        }
393
394 3
        $this->dir = $dir;
395
396 3
        return $this;
397
    }
398
399
    /**
400
     * Get IconPath
401
     *
402
     * @return null|string
403
     */
404 10
    public function getIcon(): ?string
405
    {
406 10
        return $this->icon;
407
    }
408
409
    /**
410
     * Set IconPath
411
     *
412
     * @param null|string $icon
413
     *
414
     * @return PushMessageContract
415
     */
416 5
    public function setIcon(?string $icon): PushMessageContract
417
    {
418 5
        $this->icon = $icon;
419
420 5
        return $this;
421
    }
422
423
    /**
424
     * Get Image
425
     *
426
     * @return string|null
427
     */
428 10
    public function getImage(): ?string
429
    {
430 10
        return $this->image;
431
    }
432
433
    /**
434
     * Set Image
435
     *
436
     * @param string|null $image
437
     *
438
     * @return PushMessage
439
     */
440 1
    public function setImage(?string $image): PushMessage
441
    {
442 1
        $this->image = $image;
443
444 1
        return $this;
445
    }
446
447
    /**
448
     * Get Lang
449
     *
450
     * @return null|string
451
     */
452 10
    public function getLang(): ?string
453
    {
454 10
        return $this->lang;
455
    }
456
457
    /**
458
     * Set Lang
459
     *
460
     * @param null|string $lang
461
     *
462
     * @return PushMessageContract
463
     */
464 5
    public function setLang(?string $lang): PushMessageContract
465
    {
466 5
        $this->lang = $lang;
467
468 5
        return $this;
469
    }
470
471
    /**
472
     * Get Renotify
473
     *
474
     * @return mixed
475
     */
476 11
    public function getRenotify()
477
    {
478 11
        return $this->renotify;
479
    }
480
481
    /**
482
     * Set Renotify
483
     *
484
     * @param mixed $renotify
485
     *
486
     * @return PushMessage
487
     */
488 1
    public function setRenotify($renotify): PushMessage
489
    {
490 1
        $this->renotify = $renotify;
491
492 1
        return $this;
493
    }
494
495
    /**
496
     * Get RequireInteraction
497
     *
498
     * @return mixed
499
     */
500 11
    public function getRequireInteraction()
501
    {
502 11
        return $this->require_interaction;
503
    }
504
505
    /**
506
     * Set RequireInteraction
507
     *
508
     * @param mixed $require_interaction
509
     *
510
     * @return PushMessage
511
     */
512 1
    public function setRequireInteraction($require_interaction): PushMessage
513
    {
514 1
        $this->require_interaction = $require_interaction;
515
516 1
        return $this;
517
    }
518
519
    /**
520
     * Get Silent
521
     *
522
     * @return bool
523
     */
524 10
    public function isSilent(): ?bool
525
    {
526 10
        return $this->silent;
527
    }
528
529
    /**
530
     * Set Silent
531
     *
532
     * @param bool $silent
533
     *
534
     * @return PushMessageContract
535
     */
536 3
    public function setSilent(?bool $silent): PushMessageContract
537
    {
538 3
        $this->silent = $silent;
539
540 3
        return $this;
541
    }
542
543
    /**
544
     * Get Tag
545
     *
546
     * @return null|string
547
     */
548 10
    public function getTag(): ?string
549
    {
550 10
        return $this->tag;
551
    }
552
553
    /**
554
     * Set Tag
555
     *
556
     * @param null|string $tag
557
     *
558
     * @return PushMessageContract
559
     */
560 5
    public function setTag(?string $tag): PushMessageContract
561
    {
562 5
        $this->tag = $tag;
563
564 5
        return $this;
565
    }
566
567
    /**
568
     * Get Timestamp
569
     *
570
     * @return int|null
571
     */
572 10
    public function getTimestamp(): ?int
573
    {
574 10
        return $this->timestamp;
575
    }
576
577
    /**
578
     * Set Timestamp
579
     *
580
     * @param int|null $timestamp
581
     *
582
     * @return PushMessageContract
583
     */
584 5
    public function setTimestamp(?int $timestamp): PushMessageContract
585
    {
586 5
        $this->timestamp = $timestamp;
587
588 5
        return $this;
589
    }
590
591
    /**
592
     * Get VibrationPattern
593
     *
594
     * @return array|null
595
     */
596 9
    public function getVibrate(): ?array
597
    {
598 9
        return $this->vibrate;
599
    }
600
601
    /**
602
     * Set VibrationPattern
603
     *
604
     * @param array|null $vibrate
605
     *
606
     * @return PushMessageContract
607
     */
608 7
    public function setVibrate(?array $vibrate): PushMessageContract
609
    {
610 7
        $this->vibrate = $vibrate;
611
612 7
        return $this;
613
    }
614
615
    /**
616
     * @return false|mixed|string
617
     */
618 1
    public function jsonSerialize()
619
    {
620 1
        return $this->toJson();
621
    }
622
623
    /**
624
     * Get Topic
625
     *
626
     * @return string
627
     */
628 1
    public function getTopic(): ?string
629
    {
630 1
        return $this->topic;
631
    }
632
633
    /**
634
     * Set Topic
635
     *
636
     * @param string $topic
637
     *
638
     * @return PushMessage
639
     */
640 5
    public function setTopic(?string $topic): PushMessage
641
    {
642 5
        $this->topic = $topic;
643
644 5
        return $this;
645
    }
646
647
    /**
648
     * Get Urgency
649
     *
650
     * @return string
651
     */
652 1
    public function getUrgency(): ?string
653
    {
654 1
        return $this->urgency;
655
    }
656
657
    /**
658
     * Set Urgency
659
     *
660
     * @param string $urgency
661
     *
662
     * @return PushMessage
663
     */
664 5
    public function setUrgency(?string $urgency): PushMessage
665
    {
666 5
        $this->urgency = $urgency;
667
668 5
        return $this;
669
    }
670
}
671