Completed
Pull Request — master (#26)
by
unknown
01:14
created

Payload::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 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
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
    protected function __construct()
95
    {
96
    }
97
98
    /**
99
     * @return Payload
100
     */
101
    public static function create(): Payload
102
    {
103
        return new self();
104
    }
105
106
    /**
107
     * Set Alert.
108
     *
109
     * @param Alert $alert
110
     * @return Payload
111
     */
112
    public function setAlert(Alert $alert): Payload
113
    {
114
        $this->alert = $alert;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Get Alert.
121
     *
122
     * @return Alert|null
123
     */
124
    public function getAlert()
125
    {
126
        return $this->alert;
127
    }
128
129
    /**
130
     * Set badge.
131
     *
132
     * @param int $value
133
     * @return Payload
134
     */
135
    public function setBadge(int $value): Payload
136
    {
137
        $this->badge = $value;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get badge.
144
     *
145
     * @return int|null
146
     */
147
    public function getBadge()
148
    {
149
        return $this->badge;
150
    }
151
152
    /**
153
     * Set sound.
154
     *
155
     * @param string $value
156
     * @return Payload
157
     */
158
    public function setSound(string $value): Payload
159
    {
160
        $this->sound = $value;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Get sound.
167
     *
168
     * @return string|null
169
     */
170
    public function getSound()
171
    {
172
        return $this->sound;
173
    }
174
175
    /**
176
     * Set content availability.
177
     *
178
     * @param bool $value
179
     * @return Payload
180
     */
181
    public function setContentAvailability(bool $value): Payload
182
    {
183
        $this->contentAvailable = $value;
184
185
        return $this;
186
    }
187
188
    /**
189
     * Get content availability.
190
     *
191
     * @return bool|null
192
     */
193
    public function isContentAvailable()
194
    {
195
        return $this->contentAvailable;
196
    }
197
198
    /**
199
     * Set the mutable-content key for Notification Service Extensions on iOS10.
200
     * @see http://bit.ly/mutable-content
201
     *
202
     * @param bool $value
203
     * @return Payload
204
     */
205
    public function setMutableContent(bool $value): Payload
206
    {
207
        $this->mutableContent = $value;
208
209
        return $this;
210
    }
211
212
    /**
213
     * Is content mutable.
214
     *
215
     * @return bool|null
216
     */
217
    public function hasMutableContent()
218
    {
219
        return $this->mutableContent;
220
    }
221
222
    /**
223
     * Set category.
224
     *
225
     * @param string $value
226
     * @return Payload
227
     */
228
    public function setCategory(string $value): Payload
229
    {
230
        $this->category = $value;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Get category.
237
     *
238
     * @return string|null
239
     */
240
    public function getCategory()
241
    {
242
        return $this->category;
243
    }
244
245
    /**
246
     * Set thread-id.
247
     *
248
     * @param string $value
249
     * @return Payload
250
     */
251
    public function setThreadId(string $value): Payload
252
    {
253
        $this->threadId = $value;
254
255
        return $this;
256
    }
257
258
    /**
259
     * Get thread-id.
260
     *
261
     * @return string|null
262
     */
263
    public function getThreadId()
264
    {
265
        return $this->threadId;
266
    }
267
268
    /**
269
     * Set custom value for Payload.
270
     *
271
     * @param string $key
272
     * @param mixed $value
273
     * @return Payload
274
     * @throws InvalidPayloadException
275
     */
276
    public function setCustomValue(string $key, $value): Payload
277
    {
278
        if ($key === self::PAYLOAD_ROOT_KEY) {
279
            throw InvalidPayloadException::reservedKey();
280
        }
281
282
        $this->customValues[$key] = $value;
283
284
        return $this;
285
    }
286
287
    /**
288
     * Get custom value.
289
     *
290
     * @param $key
291
     * @return mixed
292
     * @throws InvalidPayloadException
293
     */
294
    public function getCustomValue($key)
295
    {
296
        if (!array_key_exists($key, $this->customValues)) {
297
            throw InvalidPayloadException::notExistingCustomValue($key);
298
        }
299
300
        return $this->customValues[$key];
301
    }
302
303
    /**
304
     * Convert Payload to JSON.
305
     *
306
     * @return string
307
     */
308
    public function toJson(): string
309
    {
310
        return json_encode($this, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
311
    }
312
313
    /**
314
     * Specify data which should be serialized to JSON.
315
     *
316
     * @return array
317
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
318
     */
319
    public function jsonSerialize()
320
    {
321
        $payload = self::getDefaultPayloadStructure();
322
323
        if ($this->alert instanceof Alert) {
324
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_ALERT_KEY] = $this->alert;
325
        }
326
327
        if (is_int($this->badge)) {
328
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_BADGE_KEY] = $this->badge;
329
        }
330
331
        if (is_string($this->sound)) {
332
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_SOUND_KEY] = $this->sound;
333
        }
334
335
        if (is_bool($this->contentAvailable)) {
336
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
337
        }
338
339
        if (is_bool($this->mutableContent)) {
340
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
341
        }
342
343
        if (is_string($this->category)) {
344
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
345
        }
346
347
        if (is_string($this->threadId)) {
348
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_THREAD_ID_KEY] = $this->threadId;
349
        }
350
351
        if (count($this->customValues)) {
352
            $payload = array_merge($payload, $this->customValues);
353
        }
354
355
        return $payload;
356
    }
357
358
    /**
359
     * Get default payload structure.
360
     *
361
     * @return array
362
     */
363
    private static function getDefaultPayloadStructure()
364
    {
365
        return [self::PAYLOAD_ROOT_KEY => []];
366
    }
367
}
368