Completed
Branch version-1 (e85622)
by Alex
04:13
created

PushMessage::getImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 6
    public function toArray(): array
201
    {
202 6
        return array_filter([
203 6
            'title' => $this->getTitle(),
204 6
            'options' => array_filter([
205 6
                'actions' => $this->mapActionsToArray(),
206 6
                'badge' => $this->getBadge(),
207 6
                'body' => $this->getBody(),
208 6
                'data' => $this->getData(),
209 6
                'dir' => $this->getDir(),
210 6
                'icon' => $this->getIcon(),
211 6
                'image' => $this->getImage(),
212 6
                'lang' => $this->getLang(),
213 6
                'renotify' => $this->getRenotify(),
214 6
                'requireInteraction' => $this->getRequireInteraction(),
215 6
                'silent' => $this->isSilent(),
216 6
                'tag' => $this->getTag(),
217 6
                'timestamp' => $this->getTimestamp(),
218 6
                'vibrate' => !$this->isSilent() ? $this->getVibrate() : null,
219
            ]),
220
        ]);
221
    }
222
223
    /**
224
     * Get Title
225
     *
226
     * @return string
227
     */
228 7
    public function getTitle(): string
229
    {
230 7
        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 6
    private function mapActionsToArray(): ?array
251
    {
252 6
        if (!is_array($this->getActions())) {
0 ignored issues
show
introduced by
The condition is_array($this->getActions()) is always true.
Loading history...
253 6
            return null;
254
        }
255
256
        $actions = $this->getActions();
257
258
        return array_map(function (MessageActionContract $action) {
259
            return $action->toArray();
260
        }, $actions);
261
    }
262
263
    /**
264
     * Get Actions
265
     *
266
     * @return MessageActionContract[]|null
267
     */
268 6
    public function getActions(): ?array
269
    {
270 6
        return $this->actions;
271
    }
272
273
    /**
274
     * Set Actions
275
     *
276
     * @param MessageActionContract[] $actions
277
     *
278
     * @return PushMessage
279
     */
280
    public function setActions(array $actions): PushMessage
281
    {
282
        foreach ($actions as $action) {
283
            if (!$action instanceof MessageActionContract) {
284
                throw new \InvalidArgumentException(get_class($action) . ' must implement ' . MessageActionContract::class);
285
            }
286
        }
287
288
        $this->actions = $actions;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Get Badge
295
     *
296
     * @return string|null
297
     */
298 6
    public function getBadge(): ?string
299
    {
300 6
        return $this->badge;
301
    }
302
303
    /**
304
     * Set Badge
305
     *
306
     * @param string $badge
307
     *
308
     * @return PushMessage
309
     */
310
    public function setBadge(string $badge): PushMessage
311
    {
312
        $this->badge = $badge;
313
314
        return $this;
315
    }
316
317
    /**
318
     * Get Body
319
     *
320
     * @return string
321
     */
322 7
    public function getBody(): string
323
    {
324 7
        return $this->body;
325
    }
326
327
    /**
328
     * Set Body
329
     *
330
     * @param string $body
331
     *
332
     * @return PushMessageContract
333
     */
334 5
    public function setBody(string $body): PushMessageContract
335
    {
336 5
        $this->body = $body;
337
338 5
        return $this;
339
    }
340
341
    /**
342
     * Get Data
343
     *
344
     * @return mixed
345
     */
346 6
    public function getData()
347
    {
348 6
        return $this->data;
349
    }
350
351
    /**
352
     * Set Data
353
     *
354
     * @param mixed $data
355
     *
356
     * @return PushMessage
357
     */
358
    public function setData($data): PushMessage
359
    {
360
        $this->data = $data;
361
362
        return $this;
363
    }
364
365
    /**
366
     * Get Dir
367
     *
368
     * @return string
369
     */
370 6
    public function getDir(): string
371
    {
372 6
        return $this->dir;
373
    }
374
375
    /**
376
     * Set Dir
377
     *
378
     * @param string $dir
379
     *
380
     * @return PushMessage
381
     */
382
    public function setDir(string $dir): PushMessage
383
    {
384
        if (!array_has(self::NOTIFICATION_DIRECTIONS, $dir)) {
385
            throw new \InvalidArgumentException('Direction must be one of ' . implode(', ',
386
                    self::NOTIFICATION_DIRECTIONS));
387
        }
388
389
        $this->dir = $dir;
390
391
        return $this;
392
    }
393
394
    /**
395
     * Get IconPath
396
     *
397
     * @return null|string
398
     */
399 7
    public function getIcon(): ?string
400
    {
401 7
        return $this->icon;
402
    }
403
404
    /**
405
     * Set IconPath
406
     *
407
     * @param null|string $icon
408
     *
409
     * @return PushMessageContract
410
     */
411 5
    public function setIcon(?string $icon): PushMessageContract
412
    {
413 5
        $this->icon = $icon;
414
415 5
        return $this;
416
    }
417
418
    /**
419
     * Get Image
420
     *
421
     * @return string|null
422
     */
423 6
    public function getImage(): ?string
424
    {
425 6
        return $this->image;
426
    }
427
428
    /**
429
     * Set Image
430
     *
431
     * @param string|null $image
432
     *
433
     * @return PushMessage
434
     */
435
    public function setImage(?string $image): PushMessage
436
    {
437
        $this->image = $image;
438
439
        return $this;
440
    }
441
442
    /**
443
     * Get Lang
444
     *
445
     * @return null|string
446
     */
447 7
    public function getLang(): ?string
448
    {
449 7
        return $this->lang;
450
    }
451
452
    /**
453
     * Set Lang
454
     *
455
     * @param null|string $lang
456
     *
457
     * @return PushMessageContract
458
     */
459 5
    public function setLang(?string $lang): PushMessageContract
460
    {
461 5
        $this->lang = $lang;
462
463 5
        return $this;
464
    }
465
466
    /**
467
     * Get Renotify
468
     *
469
     * @return mixed
470
     */
471 6
    public function getRenotify()
472
    {
473 6
        return $this->renotify;
474
    }
475
476
    /**
477
     * Set Renotify
478
     *
479
     * @param mixed $renotify
480
     *
481
     * @return PushMessage
482
     */
483
    public function setRenotify($renotify): PushMessage
484
    {
485
        $this->renotify = $renotify;
486
487
        return $this;
488
    }
489
490
    /**
491
     * Get RequireInteraction
492
     *
493
     * @return mixed
494
     */
495 6
    public function getRequireInteraction()
496
    {
497 6
        return $this->require_interaction;
498
    }
499
500
    /**
501
     * Set RequireInteraction
502
     *
503
     * @param mixed $require_interaction
504
     *
505
     * @return PushMessage
506
     */
507
    public function setRequireInteraction($require_interaction): PushMessage
508
    {
509
        $this->require_interaction = $require_interaction;
510
511
        return $this;
512
    }
513
514
    /**
515
     * Get Silent
516
     *
517
     * @return bool
518
     */
519 7
    public function isSilent(): ?bool
520
    {
521 7
        return $this->silent;
522
    }
523
524
    /**
525
     * Set Silent
526
     *
527
     * @param bool $silent
528
     *
529
     * @return PushMessageContract
530
     */
531 3
    public function setSilent(?bool $silent): PushMessageContract
532
    {
533 3
        $this->silent = $silent;
534
535 3
        return $this;
536
    }
537
538
    /**
539
     * Get Tag
540
     *
541
     * @return null|string
542
     */
543 7
    public function getTag(): ?string
544
    {
545 7
        return $this->tag;
546
    }
547
548
    /**
549
     * Set Tag
550
     *
551
     * @param null|string $tag
552
     *
553
     * @return PushMessageContract
554
     */
555 5
    public function setTag(?string $tag): PushMessageContract
556
    {
557 5
        $this->tag = $tag;
558
559 5
        return $this;
560
    }
561
562
    /**
563
     * Get Timestamp
564
     *
565
     * @return int|null
566
     */
567 7
    public function getTimestamp(): ?int
568
    {
569 7
        return $this->timestamp;
570
    }
571
572
    /**
573
     * Set Timestamp
574
     *
575
     * @param int|null $timestamp
576
     *
577
     * @return PushMessageContract
578
     */
579 5
    public function setTimestamp(?int $timestamp): PushMessageContract
580
    {
581 5
        $this->timestamp = $timestamp;
582
583 5
        return $this;
584
    }
585
586
    /**
587
     * Get VibrationPattern
588
     *
589
     * @return array|null
590
     */
591 6
    public function getVibrate(): ?array
592
    {
593 6
        return $this->vibrate;
594
    }
595
596
    /**
597
     * Set VibrationPattern
598
     *
599
     * @param array|null $vibrate
600
     *
601
     * @return PushMessageContract
602
     */
603 7
    public function setVibrate(?array $vibrate): PushMessageContract
604
    {
605 7
        $this->vibrate = $vibrate;
606
607 7
        return $this;
608
    }
609
610
    /**
611
     * @return false|mixed|string
612
     */
613 1
    public function jsonSerialize()
614
    {
615 1
        return $this->toJson();
616
    }
617
618
    /**
619
     * Get Topic
620
     *
621
     * @return string
622
     */
623 1
    public function getTopic(): ?string
624
    {
625 1
        return $this->topic;
626
    }
627
628
    /**
629
     * Set Topic
630
     *
631
     * @param string $topic
632
     *
633
     * @return PushMessage
634
     */
635 5
    public function setTopic(?string $topic): PushMessage
636
    {
637 5
        $this->topic = $topic;
638
639 5
        return $this;
640
    }
641
642
    /**
643
     * Get Urgency
644
     *
645
     * @return string
646
     */
647 1
    public function getUrgency(): ?string
648
    {
649 1
        return $this->urgency;
650
    }
651
652
    /**
653
     * Set Urgency
654
     *
655
     * @param string $urgency
656
     *
657
     * @return PushMessage
658
     */
659 5
    public function setUrgency(?string $urgency): PushMessage
660
    {
661 5
        $this->urgency = $urgency;
662
663 5
        return $this;
664
    }
665
}
666