Completed
Push — master ( b69afb...26655b )
by Arthur
01:11
created

Payload::getUrlArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Pushok package.
5
 *
6
 * (c) Arthur Edamov <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pushok;
13
14
use Countable;
15
use Pushok\Payload\Alert;
16
17
// Polyfill for PHP 7.2
18
if (!function_exists('is_countable')) {
19
    function is_countable($var)
20
    {
21
        return (is_array($var) || $var instanceof Countable);
22
    }
23
}
24
25
/**
26
 * Class Payload
27
 *
28
 * @package Pushok
29
 *
30
 * @see     http://bit.ly/payload-key-reference
31
 */
32
class Payload implements \JsonSerializable
33
{
34
    const PAYLOAD_ROOT_KEY = 'aps';
35
    const PAYLOAD_ALERT_KEY = 'alert';
36
    const PAYLOAD_BADGE_KEY = 'badge';
37
    const PAYLOAD_SOUND_KEY = 'sound';
38
    const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available';
39
    const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content';
40
    const PAYLOAD_CATEGORY_KEY = 'category';
41
    const PAYLOAD_THREAD_ID_KEY = 'thread-id';
42
    const PAYLOAD_URL_ARGS_KEY = 'url-args';
43
44
    const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
45
    const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
46
    const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048;
47
48
    /**
49
     * The notification settings for your app on the user’s device determine whether an alert or banner is displayed.
50
     *
51
     * @var Alert|string
52
     */
53
    private $alert;
54
55
    /**
56
     * The number to display as the badge of the app icon.
57
     * If this property is absent, the badge is not changed.
58
     *
59
     * @var int
60
     */
61
    private $badge;
62
63
    /**
64
     * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container.
65
     *
66
     * @var string
67
     */
68
    private $sound;
69
70
    /**
71
     * Include this key with a value of true to configure a silent notification.
72
     *
73
     * @var bool
74
     */
75
    private $contentAvailable;
76
77
    /**
78
     * Include this key with a value of true to configure a mutable content notification.
79
     *
80
     * @var bool
81
     */
82
    private $mutableContent;
83
84
    /**
85
     * Provide this key with a string value that represents the notification’s type.
86
     *
87
     * @var string
88
     */
89
    private $category;
90
91
    /**
92
     * Provide this key with a string value that represents the app-specific identifier for grouping notifications.
93
     *
94
     * @var string
95
     */
96
    private $threadId;
97
98
    /**
99
     * Provide this key with an array value that represents the url-args for Safari notifications.
100
     *
101
     * @var string
102
     */
103
    private $urlArgs;
104
105
    /**
106
     * Payload custom values.
107
     *
108
     * @var array
109
     */
110
    private $customValues;
111
112
    /**
113
     * Push notification type
114
     *
115
     * https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns#2947607
116
     *
117
     * @var string
118
     */
119
    private $pushType;
120
121
    protected function __construct()
122
    {
123
    }
124
125
    /**
126
     * @return Payload
127
     */
128
    public static function create(): Payload
129
    {
130
        return new self();
131
    }
132
133
    /**
134
     * Get Alert.
135
     *
136
     * @return Alert|null
137
     */
138
    public function getAlert()
139
    {
140
        return $this->alert;
141
    }
142
143
    /**
144
     * Set Alert.
145
     *
146
     * @param Alert|string $alert
147
     *
148
     * @return Payload
149
     */
150
    public function setAlert($alert): Payload
151
    {
152
        if ($alert instanceof Alert || is_string($alert)) {
153
            $this->alert = $alert;
154
        }
155
156
        return $this;
157
    }
158
159
    /**
160
     * Get badge.
161
     *
162
     * @return int|null
163
     */
164
    public function getBadge()
165
    {
166
        return $this->badge;
167
    }
168
169
    /**
170
     * Set badge.
171
     *
172
     * @param int $value
173
     *
174
     * @return Payload
175
     */
176
    public function setBadge(int $value): Payload
177
    {
178
        $this->badge = $value;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Get sound.
185
     *
186
     * @return string|null
187
     */
188
    public function getSound()
189
    {
190
        return $this->sound;
191
    }
192
193
    /**
194
     * Set sound.
195
     *
196
     * @param string $value
197
     *
198
     * @return Payload
199
     */
200
    public function setSound(string $value): Payload
201
    {
202
        $this->sound = $value;
203
204
        return $this;
205
    }
206
207
    /**
208
     * Set content availability.
209
     *
210
     * @param bool $value
211
     *
212
     * @return Payload
213
     */
214
    public function setContentAvailability(bool $value): Payload
215
    {
216
        $this->contentAvailable = $value;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Get content availability.
223
     *
224
     * @return bool|null
225
     */
226
    public function isContentAvailable()
227
    {
228
        return $this->contentAvailable;
229
    }
230
231
    /**
232
     * Set the mutable-content key for Notification Service Extensions on iOS10.
233
     *
234
     * @see http://bit.ly/mutable-content
235
     *
236
     * @param bool $value
237
     *
238
     * @return Payload
239
     */
240
    public function setMutableContent(bool $value): Payload
241
    {
242
        $this->mutableContent = $value;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Is content mutable.
249
     *
250
     * @return bool|null
251
     */
252
    public function hasMutableContent()
253
    {
254
        return $this->mutableContent;
255
    }
256
257
    /**
258
     * Get category.
259
     *
260
     * @return string|null
261
     */
262
    public function getCategory()
263
    {
264
        return $this->category;
265
    }
266
267
    /**
268
     * Set category.
269
     *
270
     * @param string $value
271
     *
272
     * @return Payload
273
     */
274
    public function setCategory(string $value): Payload
275
    {
276
        $this->category = $value;
277
278
        return $this;
279
    }
280
281
    /**
282
     * Get thread-id.
283
     *
284
     * @return string|null
285
     */
286
    public function getThreadId()
287
    {
288
        return $this->threadId;
289
    }
290
291
    /**
292
     * Set thread-id.
293
     *
294
     * @param string $value
295
     *
296
     * @return Payload
297
     */
298
    public function setThreadId(string $value): Payload
299
    {
300
        $this->threadId = $value;
301
302
        return $this;
303
    }
304
305
    /**
306
     * Get url-args.
307
     *
308
     * @return array|null
309
     */
310
    public function getUrlArgs()
311
    {
312
        return $this->urlArgs;
313
    }
314
315
    /**
316
     * Set url-args.
317
     *
318
     * @param array $value
319
     *
320
     * @return Payload
321
     */
322
    public function setUrlArgs(array $value): Payload
323
    {
324
        $this->urlArgs = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type array is incompatible with the declared type string of property $urlArgs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
325
326
        return $this;
327
    }
328
329
    /**
330
     * Set custom value for Payload.
331
     *
332
     * @param string $key
333
     * @param mixed $value
334
     *
335
     * @return Payload
336
     * @throws InvalidPayloadException
337
     */
338
    public function setCustomValue(string $key, $value): Payload
339
    {
340
        if ($key === self::PAYLOAD_ROOT_KEY) {
341
            throw InvalidPayloadException::reservedKey();
342
        }
343
344
        $this->customValues[$key] = $value;
345
346
        return $this;
347
    }
348
349
    /**
350
     * Get custom value.
351
     *
352
     * @param $key
353
     *
354
     * @return mixed
355
     * @throws InvalidPayloadException
356
     */
357
    public function getCustomValue($key)
358
    {
359
        if (!array_key_exists($key, $this->customValues)) {
360
            throw InvalidPayloadException::notExistingCustomValue($key);
361
        }
362
363
        return $this->customValues[$key];
364
    }
365
366
    /**
367
     * Set push type for Payload.
368
     *
369
     * @param string $pushType
370
     * @return Payload
371
     */
372
    public function setPushType($pushType) {
373
        $this->pushType = $pushType;
374
375
        return $this;
376
    }
377
378
    /**
379
     * Get push type for Payload.
380
     *
381
     * @return string
382
     */
383
    public function getPushType() {
384
        return $this->pushType;
385
    }
386
387
    /**
388
     * Convert Payload to JSON.
389
     *
390
     * @return string
391
     */
392
    public function toJson(): string
393
    {
394
        return json_encode($this, JSON_UNESCAPED_UNICODE);
395
    }
396
397
    /**
398
     * Specify data which should be serialized to JSON.
399
     *
400
     * @return array
401
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
402
     */
403
    public function jsonSerialize()
404
    {
405
        $payload = self::getDefaultPayloadStructure();
406
407
        if ($this->alert instanceof Alert || is_string($this->alert)) {
408
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
409
        }
410
411
        if (is_int($this->badge)) {
412
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_BADGE_KEY} = $this->badge;
413
        }
414
415
        if (is_string($this->sound)) {
416
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_SOUND_KEY} = $this->sound;
417
        }
418
419
        if (is_bool($this->contentAvailable)) {
420
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CONTENT_AVAILABLE_KEY} = (int)$this->contentAvailable;
421
        }
422
423
        if (is_bool($this->mutableContent)) {
424
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_MUTABLE_CONTENT_KEY} = (int)$this->mutableContent;
425
        }
426
427
        if (is_string($this->category)) {
428
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CATEGORY_KEY} = $this->category;
429
        }
430
431
        if (is_string($this->threadId)) {
432
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId;
433
        }
434
435
        if (is_array($this->urlArgs)) {
436
            $payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_URL_ARGS_KEY} = $this->urlArgs;
437
        }
438
439
        if (is_countable($this->customValues) && count($this->customValues)) {
440
            $payload = array_merge($payload, $this->customValues);
441
        }
442
443
        return $payload;
444
    }
445
446
    /**
447
     * Get default payload structure.
448
     *
449
     * @return array
450
     */
451
    private static function getDefaultPayloadStructure()
452
    {
453
        return [self::PAYLOAD_ROOT_KEY => new \stdClass];
454
    }
455
}
456