Completed
Pull Request — master (#84)
by
unknown
01:10
created

Payload::getDefaultPayloadStructure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 Pushok\Payload\Alert;
15
16
/**
17
 * Class Payload
18
 * @package Pushok
19
 *
20
 * @see http://bit.ly/payload-key-reference
21
 */
22
class Payload implements \JsonSerializable
23
{
24
    const PAYLOAD_ROOT_KEY = 'aps';
25
    const PAYLOAD_ALERT_KEY = 'alert';
26
    const PAYLOAD_BADGE_KEY = 'badge';
27
    const PAYLOAD_SOUND_KEY = 'sound';
28
    const PAYLOAD_CONTENT_AVAILABLE_KEY = 'content-available';
29
    const PAYLOAD_MUTABLE_CONTENT_KEY = 'mutable-content';
30
    const PAYLOAD_CATEGORY_KEY = 'category';
31
    const PAYLOAD_THREAD_ID_KEY = 'thread-id';
32
33
    const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
34
    const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
35
    const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048;
36
37
    /**
38
     * The notification settings for your app on the user’s device determine whether an alert or banner is displayed.
39
     *
40
     * @var Alert|string
41
     */
42
    private $alert;
43
44
    /**
45
     * The number to display as the badge of the app icon.
46
     * If this property is absent, the badge is not changed.
47
     *
48
     * @var int
49
     */
50
    private $badge;
51
52
    /**
53
     * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container.
54
     *
55
     * @var string
56
     */
57
    private $sound;
58
59
    /**
60
     * Include this key with a value of true to configure a silent notification.
61
     *
62
     * @var bool
63
     */
64
    private $contentAvailable;
65
66
    /**
67
     * Include this key with a value of true to configure a mutable content notification.
68
     *
69
     * @var bool
70
     */
71
    private $mutableContent;
72
73
    /**
74
     * Provide this key with a string value that represents the notification’s type.
75
     *
76
     * @var string
77
     */
78
    private $category;
79
80
    /**
81
     * Provide this key with a string value that represents the app-specific identifier for grouping notifications.
82
     *
83
     * @var string
84
     */
85
    private $threadId;
86
87
    /**
88
     * Payload custom values.
89
     *
90
     * @var array
91
     */
92
    private $customValues;
93
94
    /**
95
     * Payload JSON encode options.
96
     *
97
     * @var Integer
98
     */
99
    private $jsonOptions = JSON_UNESCAPED_UNICODE;
100
101
    protected function __construct()
102
    {
103
    }
104
105
    /**
106
     * @return Payload
107
     */
108
    public static function create(): Payload
109
    {
110
        return new self();
111
    }
112
113
    /**
114
     * Set Alert.
115
     *
116
     * @param Alert|string $alert
117
     * @return Payload
118
     */
119
    public function setAlert($alert): Payload
120
    {
121
        if ($alert instanceof Alert || is_string($alert)) {
122
            $this->alert = $alert;
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get Alert.
130
     *
131
     * @return Alert|null
132
     */
133
    public function getAlert()
134
    {
135
        return $this->alert;
136
    }
137
138
    /**
139
     * Set badge.
140
     *
141
     * @param int $value
142
     * @return Payload
143
     */
144
    public function setBadge(int $value): Payload
145
    {
146
        $this->badge = $value;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Get badge.
153
     *
154
     * @return int|null
155
     */
156
    public function getBadge()
157
    {
158
        return $this->badge;
159
    }
160
161
    /**
162
     * Set sound.
163
     *
164
     * @param string $value
165
     * @return Payload
166
     */
167
    public function setSound(string $value): Payload
168
    {
169
        $this->sound = $value;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Get sound.
176
     *
177
     * @return string|null
178
     */
179
    public function getSound()
180
    {
181
        return $this->sound;
182
    }
183
184
    /**
185
     * Set content availability.
186
     *
187
     * @param bool $value
188
     * @return Payload
189
     */
190
    public function setContentAvailability(bool $value): Payload
191
    {
192
        $this->contentAvailable = $value;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Get content availability.
199
     *
200
     * @return bool|null
201
     */
202
    public function isContentAvailable()
203
    {
204
        return $this->contentAvailable;
205
    }
206
207
    /**
208
     * Set the mutable-content key for Notification Service Extensions on iOS10.
209
     * @see http://bit.ly/mutable-content
210
     *
211
     * @param bool $value
212
     * @return Payload
213
     */
214
    public function setMutableContent(bool $value): Payload
215
    {
216
        $this->mutableContent = $value;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Is content mutable.
223
     *
224
     * @return bool|null
225
     */
226
    public function hasMutableContent()
227
    {
228
        return $this->mutableContent;
229
    }
230
231
    /**
232
     * Set category.
233
     *
234
     * @param string $value
235
     * @return Payload
236
     */
237
    public function setCategory(string $value): Payload
238
    {
239
        $this->category = $value;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Get category.
246
     *
247
     * @return string|null
248
     */
249
    public function getCategory()
250
    {
251
        return $this->category;
252
    }
253
254
    /**
255
     * Set thread-id.
256
     *
257
     * @param string $value
258
     * @return Payload
259
     */
260
    public function setThreadId(string $value): Payload
261
    {
262
        $this->threadId = $value;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Get thread-id.
269
     *
270
     * @return string|null
271
     */
272
    public function getThreadId()
273
    {
274
        return $this->threadId;
275
    }
276
277
    /**
278
     * Set custom value for Payload.
279
     *
280
     * @param string $key
281
     * @param mixed $value
282
     * @return Payload
283
     * @throws InvalidPayloadException
284
     */
285
    public function setCustomValue(string $key, $value): Payload
286
    {
287
        if ($key === self::PAYLOAD_ROOT_KEY) {
288
            throw InvalidPayloadException::reservedKey();
289
        }
290
291
        $this->customValues[$key] = $value;
292
293
        return $this;
294
    }
295
296
    /**
297
     * Get custom value.
298
     *
299
     * @param $key
300
     * @return mixed
301
     * @throws InvalidPayloadException
302
     */
303
    public function getCustomValue($key)
304
    {
305
        if (!array_key_exists($key, $this->customValues)) {
306
            throw InvalidPayloadException::notExistingCustomValue($key);
307
        }
308
309
        return $this->customValues[$key];
310
    }
311
312
    /**
313
     * Set JSON Options.
314
     *
315
     * @param $options
316
     * @return mixed
317
     */
318
    public function setJSONOptions($options)
319
    {
320
        $this->jsonOptions = $options;
321
        return $this;
322
    }
323
324
    /**
325
     * Get JSON Options.
326
     *
327
     * @return Integer
328
     */
329
    public function getJSONOptions()
330
    {
331
        return $this->jsonOptions;
332
    }
333
334
    /**
335
     * Convert Payload to JSON.
336
     *
337
     * @return string
338
     */
339
    public function toJson(): string
340
    {
341
        return json_encode($this, $this->jsonOptions);
342
    }
343
344
    /**
345
     * Specify data which should be serialized to JSON.
346
     *
347
     * @return array
348
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
349
     */
350
    public function jsonSerialize()
351
    {
352
        $payload = self::getDefaultPayloadStructure();
353
354
        if ($this->alert instanceof Alert || is_string($this->alert)) {
355
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_ALERT_KEY] = $this->alert;
356
        }
357
358
        if (is_int($this->badge)) {
359
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_BADGE_KEY] = $this->badge;
360
        }
361
362
        if (is_string($this->sound)) {
363
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_SOUND_KEY] = $this->sound;
364
        }
365
366
        if (is_bool($this->contentAvailable)) {
367
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
368
        }
369
370
        if (is_bool($this->mutableContent)) {
371
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
372
        }
373
374
        if (is_string($this->category)) {
375
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
376
        }
377
378
        if (is_string($this->threadId)) {
379
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_THREAD_ID_KEY] = $this->threadId;
380
        }
381
382
        if ((is_array($this->customValues) || $this->customValues instanceof Countable) && count($this->customValues)) {
0 ignored issues
show
Bug introduced by
The class Pushok\Countable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
383
            $payload = array_merge($payload, $this->customValues);
384
        }
385
386
        return $payload;
387
    }
388
389
    /**
390
     * Get default payload structure.
391
     *
392
     * @return array
393
     */
394
    private static function getDefaultPayloadStructure()
395
    {
396
        return [self::PAYLOAD_ROOT_KEY => []];
397
    }
398
}
399