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 ( c0ae65...966c3e )
by Alexander
02:25
created

GCM::createMessage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 13
nc 4
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\Message\Type;
11
12
use Zbox\UnifiedPush\Message\MessageBase;
13
use Zbox\UnifiedPush\NotificationService\NotificationServices;
14
use Zbox\UnifiedPush\Exception\InvalidArgumentException;
15
16
/**
17
 * Class GCM
18
 * @package Zbox\UnifiedPush\Message\Type
19
 */
20
class GCM extends MessageBase
21
{
22
    /**
23
     * The maximum size allowed for an Android notification payload is 4096 bytes
24
     */
25
    const PAYLOAD_MAX_LENGTH = 4096;
26
27
    /**
28
     * It`s possible to send the same message to up to 1000 registration IDs in one request
29
     */
30
    const MAX_RECIPIENTS_PER_MESSAGE_COUNT = 1000;
31
32
    /**
33
     * Allow test the request without actually sending the message
34
     *
35
     * @var boolean
36
     */
37
    private $dryRun = false;
38
39
    /**
40
     * If there is already a message with the same collapse key (and registration ID)
41
     * stored and waiting for delivery, the old message will be discarded
42
     * and the new message will take its place
43
     *
44
     * @var string
45
     */
46
    private $collapseKey;
47
48
    /**
49
     * Message payload data
50
     *
51
     * @var array
52
     */
53
    private $payloadData;
54
55
    /**
56
     * If the device is connected but idle, the message will still be delivered right away
57
     * unless the delay_while_idle flag is set to true
58
     *
59
     * @var boolean
60
     */
61
    private $delayWhileIdle;
62
63
    /**
64
     * Package name of you application
65
     *
66
     * @var string
67
     */
68
    private $packageName;
69
70
    /**
71
     * @return string
72
     */
73
    public function getMessageType()
74
    {
75
        return NotificationServices::GOOGLE_CLOUD_MESSAGING;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getCollapseKey()
82
    {
83
        return $this->collapseKey;
84
    }
85
86
    /**
87
     * @param string $collapseKey
88
     * @return $this
89
     */
90
    public function setCollapseKey($collapseKey)
91
    {
92
        $this->collapseKey = $collapseKey;
93
        return $this;
94
    }
95
96
    /**
97
     * @return boolean
98
     */
99
    public function isDelayWhileIdle()
100
    {
101
        return $this->delayWhileIdle;
102
    }
103
104
    /**
105
     * @param boolean $delayWhileIdle
106
     * @return $this
107
     */
108
    public function setDelayWhileIdle($delayWhileIdle)
109
    {
110
        $this->delayWhileIdle = $delayWhileIdle;
111
        return $this;
112
    }
113
114
    /**
115
     * @return boolean
116
     */
117
    public function isDryRun()
118
    {
119
        return $this->dryRun;
120
    }
121
122
    /**
123
     * @param boolean $dryRun
124
     * @return $this
125
     */
126
    public function setDryRun($dryRun)
127
    {
128
        $this->dryRun = $dryRun;
129
        return $this;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getPackageName()
136
    {
137
        return $this->packageName;
138
    }
139
140
    /**
141
     * @param string $packageName
142
     * @return $this
143
     */
144
    public function setPackageName($packageName)
145
    {
146
        $this->packageName = $packageName;
147
        return $this;
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    public function getPayloadData()
154
    {
155
        return $this->payloadData;
156
    }
157
158
    /**
159
     * @param array $payloadData
160
     * @return $this
161
     */
162
    public function setPayloadData($payloadData)
163
    {
164
        $this->payloadData = $payloadData;
165
        return $this;
166
    }
167
168
    /**
169
     * @param array $recipients
170
     * @return array
171
     */
172
    public function createMessage($recipients)
173
    {
174
        $registrationIds = array();
175
176
        foreach ($recipients as $recipient) {
177
            $registrationIds[] = $recipient->getIdentifier();
178
        }
179
180
        $message = array(
181
            'collapse_key'      => $this->getCollapseKey(),
182
            'delay_while_idle'  => $this->isDelayWhileIdle(),
183
            'registration_ids'  => $registrationIds,
184
            'data'              => $this->getPayloadData(),
185
            'time_to_live'      => $this->getExpirationTime()->format('U') - time()
186
        );
187
188
        if ($this->isDryRun()) {
189
            $message['dry_run'] = true;
190
        }
191
192
        return $message;
193
    }
194
195
    /**
196
     * @param string $message
197
     * @param array $recipients
198
     * @return array
199
     */
200
    public function packMessage($message, $recipients)
201
    {
202
        return array(
203
            'body'       => $message,
204
            'recipients' => $recipients
205
        );
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function validateRecipient($token)
212
    {
213
        if (!preg_match('#^[0-9a-z\-\_]+$#i', $token)) {
214
            throw new InvalidArgumentException(sprintf(
215
                'Device token must be mask "%s". Token given: "%s"',
216
                '^[0-9a-z\-\_]+$#i',
217
                $token
218
            ));
219
        }
220
        return true;
221
    }
222
}
223