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 ( fd1b65...2c3c0a )
by Alexander
02:20
created

Response::checkResponseCode()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 22
rs 8.9197
cc 4
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\NotificationService\GCM;
11
12
use Zbox\UnifiedPush\NotificationService\ResponseInterface;
13
use Zbox\UnifiedPush\Message\RecipientDevice;
14
use Zbox\UnifiedPush\Exception\InvalidRecipientException;
15
use Zbox\UnifiedPush\Exception\DispatchMessageException;
16
use Zbox\UnifiedPush\Exception\MalformedNotificationException;
17
use Zbox\UnifiedPush\Exception\RuntimeException;
18
19
/**
20
 * Class Response
21
 * @package Zbox\UnifiedPush\NotificationService\GCM
22
 */
23
class Response implements ResponseInterface
24
{
25
    const REQUEST_HAS_SUCCEED_CODE     = 200;
26
    const MALFORMED_NOTIFICATION_CODE  = 400;
27
    const AUTHENTICATION_ERROR_CODE    = 401;
28
29
    /**
30
     * @var \Buzz\Message\Response
31
     */
32
    protected $response;
33
34
    /**
35
     * @var \ArrayIterator
36
     */
37
    protected $recipients;
38
39
    /**
40
     * @param \Buzz\Message\Response $response
41
     * @param \ArrayIterator $recipients
42
     */
43
    public function __construct(\Buzz\Message\Response $response, \ArrayIterator $recipients)
44
    {
45
        $this->response     = $response;
46
        $this->recipients   = $recipients;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function processResponse()
53
    {
54
        $response   = $this->response;
55
        $statusCode = $response->getStatusCode();
56
57
        $this->checkResponseCode($statusCode);
58
59
        $encodedMessage = $response->getContent();
60
        $message = $this->decodeMessage($encodedMessage);
61
62
        $this->checkMessageStatus($message);
63
        $this->checkMessageResult($message, $this->recipients);
64
    }
65
66
    /**
67
     * @param string $json
68
     * @return \stdClass
69
     */
70
    public function decodeMessage($json)
71
    {
72
        $message = json_decode($json);
73
74
        if (is_null($message)) {
75
            throw new DispatchMessageException("Message could not be decoded");
76
        }
77
78
        return $message;
79
    }
80
81
    /**
82
     * Checks if response has succeed code or request was rejected
83
     *
84
     * @param int $responseCode
85
     * @throws MalformedNotificationException
86
     * @throws DispatchMessageException
87
     * @throws RuntimeException
88
     */
89
    private function checkResponseCode($responseCode)
90
    {
91
        switch ($responseCode) {
92
            case self::REQUEST_HAS_SUCCEED_CODE:
93
                break;
94
95
            case self::MALFORMED_NOTIFICATION_CODE:
96
                throw new MalformedNotificationException(
97
                    "The request could not be parsed as JSON, or it contained invalid fields"
98
                );
99
100
            case self::AUTHENTICATION_ERROR_CODE:
101
                throw new DispatchMessageException(
102
                    "There was an error authenticating the sender account."
103
                );
104
105
            default:
106
                throw new RuntimeException(
107
                    "Unknown error occurred while sending notification."
108
                );
109
        }
110
    }
111
112
    /**
113
     * Checks message status
114
     *
115
     * @param \stdClass $message
116
     */
117
    private function checkMessageStatus($message)
118
    {
119
        if (!$message || $message->success == 0 || $message->failure > 0) {
120
            throw new DispatchMessageException(
121
                sprintf("%d messages could not be processed", $message->failure)
122
            );
123
        }
124
    }
125
126
    /**
127
     * Check message result
128
     *
129
     * @param \stdClass $message
130
     * @param \ArrayIterator $recipients
131
     */
132
    private function checkMessageResult($message, \ArrayIterator $recipients)
133
    {
134
        $hasDeviceError = false;
135
136
        $recipientCount = $recipients->count();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $recipientCount is correct as $recipients->count() (which targets ArrayIterator::count()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
138
        for ($i = 0; $i <= $recipientCount; $i++) {
139
            if (isset($message->results[$i]['registration_id'])) {
140
                $hasDeviceError = true;
141
                $recipients->offsetGet($i)->setIdentifierToReplaceTo($message->results[$i]['registration_id']);
142
            }
143
144
            if (isset($message->results[$i]['error'])) {
145
                $hasDeviceError = true;
146
                $error = $message->results[$i]['error'];
147
                $this->processError($error, $recipients->offsetGet($i));
148
            }
149
        }
150
151
        if ($hasDeviceError) {
152
            throw new InvalidRecipientException("Device identifier error status", $recipients);
153
        }
154
    }
155
156
    /**
157
     * @param string $error
158
     * @param RecipientDevice $recipient
159
     * @return RecipientDevice
160
     */
161
    private function processError($error, RecipientDevice $recipient)
162
    {
163
        switch ($error) {
164
            case 'InvalidRegistration':
165
            case 'NotRegistered':
166
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED);
167
                break;
168
169
            case 'Unavailable':
170
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_READY);
171
                break;
172
173
            default:
174
                break;
175
        }
176
177
        return $recipient;
178
    }
179
}
180