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.
Completed
Push — master ( 6c5ffd...9c9abb )
by Alexander
02:23
created

APNS::packPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 14
nc 1
nop 1
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\Notification\PayloadHandler;
11
12
use Zbox\UnifiedPush\Message\MessageInterface;
13
use Zbox\UnifiedPush\Message\Type\APNS as APNSMessage;
14
use Zbox\UnifiedPush\Notification\PayloadHandler;
15
use Zbox\UnifiedPush\Utils\JsonEncoder;
16
17
/**
18
 * Class APNS
19
 * @package Zbox\UnifiedPush\Notification\PayloadHandler
20
 */
21
class APNS extends PayloadHandler
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
     * @param MessageInterface $message
31
     * @return bool
32
     */
33
    public function isSupported(MessageInterface $message)
34
    {
35
        return $message instanceof APNSMessage;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function createPayload()
42
    {
43
        /** @var APNSMessage $message */
44
        $message = $this->message;
45
46
        $payload = array(
47
            'aps' => array(
48
                'alert' => $message->getAlert(),
49
                'badge' => $message->getBadge(),
50
                'sound' => $message->getSound(),
51
                'category' => $message->getCategory(),
52
            )
53
        );
54
55
        if ($message->isContentAvailable() === true) {
56
            $payload['aps']['content-available'] = 1;
57
        }
58
59
        return array_merge($payload, $message->getCustomPayloadData());
60
    }
61
62
    /**
63
     * Pack message body into binary string
64
     *
65
     * @param array $payload
66
     * @return string
67
     */
68
    public function packPayload($payload)
69
    {
70
        $payload = JsonEncoder::jsonEncode($payload);
71
72
        /** @var APNSMessage $message */
73
        $message = $this->message;
74
75
        $recipientId = $message->getRecipientDeviceCollection()->current()->getIdentifier();
76
77
        $messageRecipientId = $message->getMessageIdentifier() . '_' . $recipientId;
78
79
        $packedPayload =
80
            pack('C', 1). // Command push
81
            pack('N', $messageRecipientId).
82
            pack('N', $message->getExpirationTime()->format('U')).
83
            pack('n', 32). // Token binary length
84
            pack('H*', $recipientId);
85
        pack('n', strlen($payload));
86
87
        $packedPayload .= $payload;
88
89
        return $packedPayload;
90
    }
91
}
92