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.

Response::processResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
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 RuntimeException("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
                    self::MALFORMED_NOTIFICATION_CODE
99
                );
100
101
            case self::AUTHENTICATION_ERROR_CODE:
102
                throw new DispatchMessageException(
103
                    "There was an error authenticating the sender account.",
104
                    self::AUTHENTICATION_ERROR_CODE
105
                );
106
107
            default:
108
                throw new RuntimeException(
109
                    "Unknown error occurred while sending notification."
110
                );
111
        }
112
    }
113
114
    /**
115
     * Checks message status
116
     *
117
     * @param \stdClass $message
118
     */
119
    private function checkMessageStatus($message)
120
    {
121
        if (!$message || $message->success == 0 || $message->failure > 0) {
122
            throw new DispatchMessageException(
123
                sprintf("%d messages could not be processed", $message->failure)
124
            );
125
        }
126
    }
127
128
    /**
129
     * Check message result
130
     *
131
     * @param \stdClass $message
132
     * @param \ArrayIterator $recipients
133
     */
134
    private function checkMessageResult($message, \ArrayIterator $recipients)
135
    {
136
        $hasDeviceError = false;
137
138
        $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...
139
140
        for ($i = 0; $i <= $recipientCount; $i++) {
141
            if (isset($message->results[$i]['registration_id'])) {
142
                $hasDeviceError = true;
143
                $recipients->offsetGet($i)->setIdentifierToReplaceTo($message->results[$i]['registration_id']);
144
            }
145
146
            if (isset($message->results[$i]['error'])) {
147
                $hasDeviceError = true;
148
                $error = $message->results[$i]['error'];
149
                $this->processError($error, $recipients->offsetGet($i));
150
            }
151
        }
152
153
        if ($hasDeviceError) {
154
            throw new InvalidRecipientException("Device identifier error status", $recipients);
155
        }
156
    }
157
158
    /**
159
     * @param string $error
160
     * @param RecipientDevice $recipient
161
     * @return RecipientDevice
162
     */
163
    private function processError($error, RecipientDevice $recipient)
164
    {
165
        switch ($error) {
166
            case 'InvalidRegistration':
167
            case 'NotRegistered':
168
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED);
169
                break;
170
171
            case 'Unavailable':
172
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_READY);
173
                break;
174
175
            default:
176
                break;
177
        }
178
179
        return $recipient;
180
    }
181
}
182