GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch master (98970a)
by Alexander
02:59 queued 21s
created

APNS::packMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
/*
4
 * (c) Alexander Zhukov <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Zbox\UnifiedPush\Message\Type;
11
12
use Zbox\UnifiedPush\Message\MessageBase;
13
use Zbox\UnifiedPush\NotificationService\NotificationServices;
14
use Zbox\UnifiedPush\Exception\InvalidArgumentException;
15
use Zbox\UnifiedPush\Utils\JsonEncoder;
16
17
/**
18
 * Class APNS
19
 * @package Zbox\UnifiedPush\Message\Type
20
 */
21
class APNS extends MessageBase
22
{
23
    /**
24
     * The maximum size allowed for an iOS notification payload is 2 kilobytes
25
     * Prior to iOS 8 and in OS X, the maximum payload size is 256 bytes
26
     */
27
    const PAYLOAD_MAX_LENGTH = 2048;
28
29
    /**
30
     * APNs does not support multicast sending
31
     */
32
    const MAX_RECIPIENTS_PER_MESSAGE_COUNT = 1;
33
34
    /**
35
     * The message’s priority. Provide one of the following values:
36
     * - 10 The push message is sent immediately
37
     * - 5 The push message is sent at a time that conserves power on the device receiving it
38
     *
39
     * @var integer
40
     */
41
    protected $priority;
42
43
    /**
44
     * Message text of an alert
45
     *
46
     * @var string
47
     */
48
    private $alert;
49
50
    /**
51
     * The number to display as the badge of the application icon
52
     *
53
     * @var integer
54
     */
55
    private $badge;
56
57
    /**
58
     * The name of a sound file in the application bundle
59
     *
60
     * @var string
61
     */
62
    private $sound;
63
64
    /**
65
     * Category option for custom notification actions (iOS 8+)
66
     *
67
     * @var string
68
     */
69
    private $category;
70
71
    /**
72
     * Provide this key with a value of 1 to indicate that new content is available
73
     *
74
     * @var bool
75
     */
76
    private $contentAvailable;
77
78
    /**
79
     * Custom properties
80
     *
81
     * @var array
82
     */
83
    private $customPayloadData = array();
84
85
    /**
86
     * Gets message type
87
     *
88
     * @return string
89
     */
90
    public function getMessageType()
91
    {
92
        return NotificationServices::APPLE_PUSH_NOTIFICATIONS_SERVICE;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getAlert()
99
    {
100
        return $this->alert;
101
    }
102
103
    /**
104
     * @param string $alert
105
     * @return $this
106
     */
107
    public function setAlert($alert)
108
    {
109
        if (!is_scalar($alert)) {
110
            $this->invalidArgumentException('Alert', 'a string');
111
        }
112
        $this->alert = $alert;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return int
119
     */
120
    public function getBadge()
121
    {
122
        return $this->badge;
123
    }
124
125
    /**
126
     * @param int $badge
127
     * @return $this
128
     */
129
    public function setBadge($badge)
130
    {
131
        if (!is_int($badge)) {
132
            $this->invalidArgumentException('Badge', 'an integer');
133
        }
134
135
        $this->badge = $badge;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function getCustomPayloadData()
144
    {
145
        return $this->customPayloadData;
146
    }
147
148
    /**
149
     * @param array $customPayloadData
150
     */
151
    public function setCustomPayloadData($customPayloadData)
152
    {
153
        $this->customPayloadData = $customPayloadData;
154
    }
155
156
    /**
157
     * @return boolean
158
     */
159
    public function isContentAvailable()
160
    {
161
        return $this->contentAvailable;
162
    }
163
164
    /**
165
     * @param boolean $contentAvailable
166
     * @return $this
167
     */
168
    public function setContentAvailable($contentAvailable)
169
    {
170
        if (!is_bool($contentAvailable)) {
171
            $this->invalidArgumentException('ContentAvailable', 'a boolean');
172
        }
173
174
        $this->contentAvailable = $contentAvailable;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return int
181
     */
182
    public function getPriority()
183
    {
184
        return $this->priority;
185
    }
186
187
    /**
188
     * @param int $priority
189
     */
190
    public function setPriority($priority)
191
    {
192
        $this->priority = $priority;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getSound()
199
    {
200
        return $this->sound;
201
    }
202
203
    /**
204
     * @param string $sound
205
     * @return $this
206
     */
207
    public function setSound($sound)
208
    {
209
        if (!is_scalar($sound)) {
210
            $this->invalidArgumentException('Sound', 'an string');
211
        }
212
        $this->sound = $sound;
213
214
        return $this;
215
    }
216
217
    /**
218
     * @return string
219
     */
220
    public function getCategory()
221
    {
222
        return $this->category;
223
    }
224
225
    /**
226
     * @param string $category
227
     * @return $this
228
     */
229
    public function setCategory($category)
230
    {
231
        if (!is_scalar($category)) {
232
            $this->invalidArgumentException('Category', 'an string');
233
        }
234
        $this->category = $category;
235
236
        return $this;
237
    }
238
239
    /**
240
     * @return array
241
     */
242
    public function createPayload()
243
    {
244
        $payload = array(
245
            'aps' => array(
246
                'alert' => $this->getAlert(),
247
                'badge' => $this->getBadge(),
248
                'sound' => $this->getSound(),
249
                'category' => $this->getCategory(),
250
            )
251
        );
252
253
        if ($this->isContentAvailable() === true) {
254
            $payload['aps']['content-available'] = 1;
255
        }
256
257
        return array_merge($payload, $this->getCustomPayloadData());
258
    }
259
260
    /**
261
     * Pack message body into binary string
262
     *
263
     * @param array $payload
264
     * @return string
265
     */
266
    public function packPayload($payload)
267
    {
268
        $payload = JsonEncoder::jsonEncode($payload);
269
270
        $recipientId = $this->getRecipientDevice()->getIdentifier();
271
272
        $messageRecipientId = $this->getMessageIdentifier() . '_' . $recipientId;
273
274
        $packedPayload =
275
            pack('C', 1). // Command push
276
            pack('N', $messageRecipientId).
277
            pack('N', $this->getExpirationTime()->format('U')).
278
            pack('n', 32). // Token binary length
279
            pack('H*', $recipientId);
280
        pack('n', strlen($payload));
281
282
        $packedPayload .= $payload;
283
284
        return $packedPayload;
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290
    public function validateRecipient($token)
291
    {
292
        if (!ctype_xdigit($token)) {
293
            throw new InvalidArgumentException(sprintf(
294
                'Device token must be a hexadecimal digit. Token given: "%s"',
295
                $token
296
            ));
297
        }
298
299
        if (strlen($token) != 64) {
300
            throw new InvalidArgumentException(sprintf(
301
                'Device token must be a 64 charsets, Token length given: %d.',
302
                mb_strlen($token)
303
            ));
304
        }
305
        return true;
306
    }
307
}
308