Completed
Push — master ( 48c4c4...9a2426 )
by Alex
10:10 queued 05:00
created

PushMessage::setVibrationPattern()   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\PushMessageContract;
6
use AlexLisenkov\LaravelWebPush\Contracts\PushSubscriptionContract;
7
use AlexLisenkov\LaravelWebPush\Contracts\WebPushContract;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use Illuminate\Support\Facades\App;
10
11
abstract class PushMessage implements PushMessageContract
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $title = '';
17
    /**
18
     * @var string
19
     */
20
    protected $body = '';
21
    /**
22
     * @var null|string
23
     */
24
    protected $iconPath;
25
    /**
26
     * @var string|null
27
     */
28
    protected $urgency;
29
    /**
30
     * @var string|null
31
     */
32
    protected $topic;
33
    /**
34
     * @var string|null
35
     */
36
    protected $tag;
37
    /**
38
     * https://w3c.github.io/vibration/#idl-def-vibratepattern
39
     * array with 3 indexes
40
     * delay, time to vibrate, sleep
41
     *
42
     * @var array|null
43
     */
44
    protected $vibration_pattern = [0, 200, 1000];
45
    /**
46
     * @var int|null
47
     */
48
    protected $timestamp;
49
    /**
50
     * @var string|null
51
     */
52
    protected $lang;
53
    /**
54
     * @var bool
55
     */
56
    protected $silent = false;
57
    /**
58
     * @var string
59
     */
60
    protected $queue;
61
62
    /**
63
     * @param PushSubscriptionContract $push_subscription
64
     *
65
     * @return PromiseInterface
66
     */
67 1
    public function sendTo(PushSubscriptionContract $push_subscription): PromiseInterface
68
    {
69
        /** @var WebPushContract $web_push */
70 1
        $web_push = App::make(WebPushContract::class);
71
72 1
        return $web_push->sendMessage($this, $push_subscription);
73
    }
74
75
    /**
76
     * Get Title
77
     *
78
     * @return string
79
     */
80 7
    public function getTitle(): string
81
    {
82 7
        return $this->title;
83
    }
84
85
    /**
86
     * Set Title
87
     *
88
     * @param string $title
89
     *
90
     * @return PushMessageContract
91
     */
92 5
    public function setTitle(string $title): PushMessageContract
93
    {
94 5
        $this->title = $title;
95
96 5
        return $this;
97
    }
98
99
    /**
100
     * Get Body
101
     *
102
     * @return string
103
     */
104 7
    public function getBody(): string
105
    {
106 7
        return $this->body;
107
    }
108
109
    /**
110
     * Set Body
111
     *
112
     * @param string $body
113
     *
114
     * @return PushMessageContract
115
     */
116 5
    public function setBody(string $body): PushMessageContract
117
    {
118 5
        $this->body = $body;
119
120 5
        return $this;
121
    }
122
123
    /**
124
     * Get IconPath
125
     *
126
     * @return null|string
127
     */
128 7
    public function getIconPath(): ?string
129
    {
130 7
        return $this->iconPath;
131
    }
132
133
    /**
134
     * Set IconPath
135
     *
136
     * @param null|string $iconPath
137
     *
138
     * @return PushMessageContract
139
     */
140 5
    public function setIconPath(?string $iconPath): PushMessageContract
141
    {
142 5
        $this->iconPath = $iconPath;
143
144 5
        return $this;
145
    }
146
147
    /**
148
     * Get Urgency
149
     *
150
     * @return string
151
     */
152 7
    public function getUrgency(): ?string
153
    {
154 7
        return $this->urgency;
155
    }
156
157
    /**
158
     * Set Urgency
159
     *
160
     * @param string $urgency
161
     *
162
     * @return PushMessageContract
163
     */
164 5
    public function setUrgency(string $urgency): PushMessageContract
165
    {
166 5
        $this->urgency = $urgency;
167
168 5
        return $this;
169
    }
170
171
    /**
172
     * Get Topic
173
     *
174
     * @return string
175
     */
176 7
    public function getTopic(): ?string
177
    {
178 7
        return $this->topic;
179
    }
180
181
    /**
182
     * Set Topic
183
     *
184
     * @param string $topic
185
     *
186
     * @return PushMessageContract
187
     */
188 5
    public function setTopic(string $topic): PushMessageContract
189
    {
190 5
        $this->topic = $topic;
191
192 5
        return $this;
193
    }
194
195
    /**
196
     * Get Tag
197
     *
198
     * @return null|string
199
     */
200 7
    public function getTag(): ?string
201
    {
202 7
        return $this->tag;
203
    }
204
205
    /**
206
     * Set Tag
207
     *
208
     * @param null|string $tag
209
     *
210
     * @return PushMessageContract
211
     */
212 5
    public function setTag(?string $tag): PushMessageContract
213
    {
214 5
        $this->tag = $tag;
215
216 5
        return $this;
217
    }
218
219
    /**
220
     * Get VibrationPattern
221
     *
222
     * @return array|null
223
     */
224 6
    public function getVibrationPattern(): ?array
225
    {
226 6
        return $this->vibration_pattern;
227
    }
228
229
    /**
230
     * Set VibrationPattern
231
     *
232
     * @param array|null $vibration_pattern
233
     *
234
     * @return PushMessageContract
235
     */
236 7
    public function setVibrationPattern(?array $vibration_pattern): PushMessageContract
237
    {
238 7
        $this->vibration_pattern = $vibration_pattern;
239
240 7
        return $this;
241
    }
242
243
    /**
244
     * Get Timestamp
245
     *
246
     * @return int|null
247
     */
248 7
    public function getTimestamp(): ?int
249
    {
250 7
        return $this->timestamp;
251
    }
252
253
    /**
254
     * Set Timestamp
255
     *
256
     * @param int|null $timestamp
257
     *
258
     * @return PushMessageContract
259
     */
260 5
    public function setTimestamp(?int $timestamp): PushMessageContract
261
    {
262 5
        $this->timestamp = $timestamp;
263
264 5
        return $this;
265
    }
266
267
    /**
268
     * Get Lang
269
     *
270
     * @return null|string
271
     */
272 7
    public function getLang(): ?string
273
    {
274 7
        return $this->lang;
275
    }
276
277
    /**
278
     * Set Lang
279
     *
280
     * @param null|string $lang
281
     *
282
     * @return PushMessageContract
283
     */
284 5
    public function setLang(?string $lang): PushMessageContract
285
    {
286 5
        $this->lang = $lang;
287
288 5
        return $this;
289
    }
290
291
    /**
292
     * Get Silent
293
     *
294
     * @return bool
295
     */
296 7
    public function isSilent(): bool
297
    {
298 7
        return $this->silent;
299
    }
300
301
    /**
302
     * Set Silent
303
     *
304
     * @param bool $silent
305
     *
306
     * @return PushMessageContract
307
     */
308 3
    public function setSilent(bool $silent): PushMessageContract
309
    {
310 3
        $this->silent = $silent;
311
312 3
        return $this;
313
    }
314
315
    /**
316
     * @return array
317
     */
318 6
    public function toArray(): array
319
    {
320 6
        return array_filter([
321 6
            'title' => $this->getTitle(),
322 6
            'body' => $this->getBody(),
323 6
            'icon' => $this->getIconPath(),
324 6
            'urgency' => $this->getUrgency(),
325 6
            'topic' => $this->getTopic(),
326 6
            'tag' => $this->getTag(),
327 6
            'vibrate' => !$this->isSilent() ? $this->getVibrationPattern() : null,
328 6
            'timestamp' => $this->getTimestamp(),
329 6
            'silent' => $this->isSilent(),
330 6
            'lang' => $this->getLang(),
331
        ]);
332
    }
333
334
    /**
335
     * @param int $options
336
     *
337
     * @return false|string
338
     */
339 3
    public function toJson($options = 0)
340
    {
341 3
        return json_encode($this->toArray(), $options);
342
    }
343
344
    /**
345
     * @return string
346
     */
347 1
    public function __toString(): string
348
    {
349 1
        if( $string = $this->toJson() ){
350 1
            return (string) $string;
351
        }
352
353
        return '';
354
    }
355
356
    /**
357
     * @return false|mixed|string
358
     */
359 1
    public function jsonSerialize()
360
    {
361 1
        return $this->toJson();
362
    }
363
}
364