Completed
Push — master ( 3407d5...f79b8d )
by Arthur
12:50
created

Payload::setContentAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
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_CATEGORY_KEY = 'category';
30
    const PAYLOAD_THREAD_ID_KEY = 'thread-id';
31
32
    const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
33
    const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
34
    const PAYLOAD_BINARY_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 2048;
35
36
    /**
37
     * The notification settings for your app on the user’s device determine whether an alert or banner is displayed.
38
     *
39
     * @var Alert
40
     */
41
    private $alert;
42
43
    /**
44
     * The number to display as the badge of the app icon.
45
     * If this property is absent, the badge is not changed.
46
     *
47
     * @var int
48
     */
49
    private $badge;
50
51
    /**
52
     * The name of a sound file in the app bundle or in the Library/Sounds folder of the app’s data container.
53
     *
54
     * @var string
55
     */
56
    private $sound;
57
58
    /**
59
     * Include this key with a value of true to configure a silent notification.
60
     *
61
     * @var bool
62
     */
63
    private $contentAvailable;
64
65
    /**
66
     * Provide this key with a string value that represents the notification’s type.
67
     *
68
     * @var string
69
     */
70
    private $category;
71
72
    /**
73
     * Provide this key with a string value that represents the app-specific identifier for grouping notifications.
74
     *
75
     * @var string
76
     */
77
    private $threadId;
78
79
    /**
80
     * Payload custom values.
81
     *
82
     * @var array
83
     */
84
    private $customValues;
85
86
    protected function __constructor()
87
    {
88
    }
89
90
    /**
91
     * @return Payload
92
     */
93
    public static function create(): Payload
94
    {
95
        return new self();
96
    }
97
98
    /**
99
     * Set Alert.
100
     *
101
     * @param Alert $alert
102
     * @return Payload
103
     */
104
    public function setAlert(Alert $alert): Payload
105
    {
106
        $this->alert = $alert;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Get Alert.
113
     *
114
     * @return Alert|null
115
     */
116
    public function getAlert()
117
    {
118
        return $this->alert;
119
    }
120
121
    /**
122
     * Set badge.
123
     *
124
     * @param int $value
125
     * @return Payload
126
     */
127
    public function setBadge(int $value): Payload
128
    {
129
        $this->badge = $value;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Get badge.
136
     *
137
     * @return int|null
138
     */
139
    public function getBadge()
140
    {
141
        return $this->badge;
142
    }
143
144
    /**
145
     * Set sound.
146
     *
147
     * @param string $value
148
     * @return Payload
149
     */
150
    public function setSound(string $value): Payload
151
    {
152
        $this->sound = $value;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Get sound.
159
     *
160
     * @return string|null
161
     */
162
    public function getSound()
163
    {
164
        return $this->sound;
165
    }
166
167
    /**
168
     * Set content availability.
169
     *
170
     * @param bool $value
171
     * @return Payload
172
     */
173
    public function setContentAvailable(bool $value): Payload
174
    {
175
        $this->contentAvailable = $value;
176
177
        return $this;
178
    }
179
180
    /**
181
     * Get content availability.
182
     *
183
     * @return bool|null
184
     */
185
    public function isContentAvailable()
186
    {
187
        return $this->contentAvailable;
188
    }
189
190
    /**
191
     * Set category.
192
     *
193
     * @param string $value
194
     * @return Payload
195
     */
196
    public function setCategory(string $value): Payload
197
    {
198
        $this->category = $value;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Get category.
205
     *
206
     * @return string|null
207
     */
208
    public function getCategory()
209
    {
210
        return $this->category;
211
    }
212
213
    /**
214
     * Set thread-id.
215
     *
216
     * @param string $value
217
     * @return Payload
218
     */
219
    public function setThreadId(string $value): Payload
220
    {
221
        $this->threadId = $value;
222
223
        return $this;
224
    }
225
226
    /**
227
     * Get thread-id.
228
     *
229
     * @return string|null
230
     */
231
    public function getThreadId()
232
    {
233
        return $this->threadId;
234
    }
235
236
    /**
237
     * Set custom value for Payload.
238
     *
239
     * @param string $key
240
     * @param mixed $value
241
     * @return Payload
242
     * @throws InvalidPayloadException
243
     */
244
    public function setCustomValue(string $key, $value): Payload
245
    {
246
        if ($key === self::PAYLOAD_ROOT_KEY) {
247
            throw InvalidPayloadException::reservedKey();
248
        }
249
250
        $this->customValues[$key] = $value;
251
252
        return $this;
253
    }
254
255
    /**
256
     * Get custom value.
257
     *
258
     * @param $key
259
     * @return mixed
260
     * @throws InvalidPayloadException
261
     */
262
    public function getCustomValue($key)
263
    {
264
        if (!array_key_exists($key, $this->customValues)) {
265
            throw InvalidPayloadException::notExistingCustomValue($key);
266
        }
267
268
        return $this->customValues[$key];
269
    }
270
271
    /**
272
     * Convert Payload to JSON.
273
     *
274
     * @return string
275
     */
276
    public function toJson(): string
277
    {
278
        $payload = $this->transform();
279
280
        return json_encode($payload, defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0);
281
    }
282
283
    /**
284
     * Transform Payload object to array.
285
     *
286
     * @return array
287
     */
288
    private function transform(): array
289
    {
290
        $payload = self::getDefaultPayloadStructure();
291
292
        if ($this->alert instanceof Alert) {
293
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_ALERT_KEY] = $this->alert;
294
        }
295
296
        if (is_int($this->badge)) {
297
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_BADGE_KEY] = $this->badge;
298
        }
299
300
        if (is_string($this->sound)) {
301
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_SOUND_KEY] = $this->sound;
302
        }
303
304
        if (is_bool($this->contentAvailable)) {
305
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
306
        }
307
308
        if (is_string($this->category)) {
309
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
310
        }
311
312
        if (is_string($this->threadId)) {
313
            $payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_THREAD_ID_KEY] = $this->threadId;
314
        }
315
316
        if (count($this->customValues)) {
317
            $payload = array_merge($payload, $this->customValues);
318
        }
319
320
        return $payload;
321
    }
322
323
    private static function getDefaultPayloadStructure()
324
    {
325
        return [self::PAYLOAD_ROOT_KEY => []];
326
    }
327
}
328