Completed
Pull Request — master (#26)
by
unknown
01:25
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
     * Set mutable content.
190
     *
191
     * @param bool $value
192
     * @return Payload
193
     */
194
    public function setMutableContent(bool $value): Payload
195
    {
196
        $this->mutableContent = $value;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Get content availability.
203
     *
204
     * @return bool|null
205
     */
206
    public function isContentAvailable()
207
    {
208
        return $this->contentAvailable;
209
    }
210
211
    /**
212
     * Set category.
213
     *
214
     * @param string $value
215
     * @return Payload
216
     */
217
    public function setCategory(string $value): Payload
218
    {
219
        $this->category = $value;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Get category.
226
     *
227
     * @return string|null
228
     */
229
    public function getCategory()
230
    {
231
        return $this->category;
232
    }
233
234
    /**
235
     * Set thread-id.
236
     *
237
     * @param string $value
238
     * @return Payload
239
     */
240
    public function setThreadId(string $value): Payload
241
    {
242
        $this->threadId = $value;
243
244
        return $this;
245
    }
246
247
    /**
248
     * Get thread-id.
249
     *
250
     * @return string|null
251
     */
252
    public function getThreadId()
253
    {
254
        return $this->threadId;
255
    }
256
257
    /**
258
     * Set custom value for Payload.
259
     *
260
     * @param string $key
261
     * @param mixed $value
262
     * @return Payload
263
     * @throws InvalidPayloadException
264
     */
265
    public function setCustomValue(string $key, $value): Payload
266
    {
267
        if ($key === self::PAYLOAD_ROOT_KEY) {
268
            throw InvalidPayloadException::reservedKey();
269
        }
270
271
        $this->customValues[$key] = $value;
272
273
        return $this;
274
    }
275
276
    /**
277
     * Get custom value.
278
     *
279
     * @param $key
280
     * @return mixed
281
     * @throws InvalidPayloadException
282
     */
283
    public function getCustomValue($key)
284
    {
285
        if (!array_key_exists($key, $this->customValues)) {
286
            throw InvalidPayloadException::notExistingCustomValue($key);
287
        }
288
289
        return $this->customValues[$key];
290
    }
291
292
    /**
293
     * Convert Payload to JSON.
294
     *
295
     * @return string
296
     */
297
    public function toJson(): string
298
    {
299
        return json_encode($this, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
300
    }
301
302
    /**
303
     * Specify data which should be serialized to JSON.
304
     *
305
     * @return array
306
     * @link   http://php.net/manual/en/jsonserializable.jsonserialize.php
307
     */
308
    public function jsonSerialize()
309
    {
310
        $payload = self::getDefaultPayloadStructure();
311
312
        if ($this->alert instanceof Alert) {
313
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_ALERT_KEY] = $this->alert;
314
        }
315
316
        if (is_int($this->badge)) {
317
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_BADGE_KEY] = $this->badge;
318
        }
319
320
        if (is_string($this->sound)) {
321
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_SOUND_KEY] = $this->sound;
322
        }
323
324
        if (is_bool($this->contentAvailable)) {
325
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
326
        }
327
328
        if (is_bool($this->mutableContent)) {
329
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
330
        }
331
332
        if (is_string($this->category)) {
333
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
334
        }
335
336
        if (is_string($this->threadId)) {
337
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_THREAD_ID_KEY] = $this->threadId;
338
        }
339
340
        if (count($this->customValues)) {
341
            $payload = array_merge($payload, $this->customValues);
342
        }
343
344
        return $payload;
345
    }
346
347
    /**
348
     * Get default payload structure.
349
     *
350
     * @return array
351
     */
352
    private static function getDefaultPayloadStructure()
353
    {
354
        return [self::PAYLOAD_ROOT_KEY => []];
355
    }
356
}
357