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

Response::parseResponseMessage()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 17
nc 11
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Response::checkMessageStatus() 0 8 4
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\Message\RecipientDevice;
13
use Zbox\UnifiedPush\Exception\InvalidRecipientException;
14
use Zbox\UnifiedPush\Exception\DispatchMessageException;
15
use Zbox\UnifiedPush\Exception\MalformedNotificationException;
16
use Zbox\UnifiedPush\Exception\RuntimeException;
17
18
/**
19
 * Class Response
20
 * @package Zbox\UnifiedPush\NotificationService\GCM
21
 */
22
class Response
23
{
24
    const REQUEST_HAS_SUCCEED_CODE     = 200;
25
    const MALFORMED_NOTIFICATION_CODE  = 400;
26
    const AUTHENTICATION_ERROR_CODE    = 401;
27
28
    /**
29
     * @param \Buzz\Message\Response $response
30
     * @param \ArrayIterator $recipients
31
     */
32
    public function __construct(\Buzz\Message\Response $response, \ArrayIterator $recipients)
33
    {
34
        $statusCode = $response->getStatusCode();
35
        $this->checkResponseCode($statusCode);
36
37
        $encodedMessage = $response->getContent();
38
        $message = $this->decodeMessage($encodedMessage);
39
40
        $this->checkMessageStatus($message);
41
        $this->checkMessageResult($message, $recipients);
42
    }
43
44
    /**
45
     * @param string $json
46
     * @return \stdClass
47
     */
48
    public function decodeMessage($json)
49
    {
50
        $message = json_decode($json);
51
52
        if (is_null($message)) {
53
            throw new DispatchMessageException("Message could not be decoded");
54
        }
55
56
        return $message;
57
    }
58
59
    /**
60
     * Checks if response has succeed code or request was rejected
61
     *
62
     * @param int $responseCode
63
     * @throws MalformedNotificationException
64
     * @throws DispatchMessageException
65
     * @throws RuntimeException
66
     */
67
    private function checkResponseCode($responseCode)
68
    {
69
        switch ($responseCode) {
70
            case self::REQUEST_HAS_SUCCEED_CODE:
71
                break;
72
73
            case self::MALFORMED_NOTIFICATION_CODE:
74
                throw new MalformedNotificationException(
75
                    "The request could not be parsed as JSON, or it contained invalid fields"
76
                );
77
78
            case self::AUTHENTICATION_ERROR_CODE:
79
                throw new DispatchMessageException(
80
                    "There was an error authenticating the sender account."
81
                );
82
83
            default:
84
                throw new RuntimeException(
85
                    "Unknown error occurred while sending notification."
86
                );
87
        }
88
    }
89
90
    /**
91
     * Checks message status
92
     *
93
     * @param \stdClass $message
94
     */
95
    private function checkMessageStatus($message)
96
    {
97
        if (!$message || $message->success == 0 || $message->failure > 0) {
98
            throw new DispatchMessageException(
99
                sprintf("%d messages could not be processed", $message->failure)
100
            );
101
        }
102
    }
103
104
    /**
105
     * Check message result
106
     *
107
     * @param \stdClass $message
108
     * @param \ArrayIterator $recipients
109
     */
110
    private function checkMessageResult($message, \ArrayIterator $recipients)
111
    {
112
        $hasDeviceError = false;
113
114
        $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...
115
116
        for ($i = 0; $i <= $recipientCount; $i++) {
117
            if (isset($message->results[$i]['registration_id'])) {
118
                $hasDeviceError = true;
119
                $recipients->offsetGet($i)->setIdentifierToReplaceTo($message->results[$i]['registration_id']);
120
            }
121
122
            if (isset($message->results[$i]['error'])) {
123
                $hasDeviceError = true;
124
                $error = $message->results[$i]['error'];
125
                $this->processError($error, $recipients->offsetGet($i));
126
            }
127
        }
128
129
        if ($hasDeviceError) {
130
            throw new InvalidRecipientException("Device identifier error status", $recipients);
131
        }
132
    }
133
134
    /**
135
     * @param string $error
136
     * @param RecipientDevice $recipient
137
     * @return RecipientDevice
138
     */
139
    private function processError($error, RecipientDevice $recipient)
140
    {
141
        switch ($error) {
142
            case 'InvalidRegistration':
143
            case 'NotRegistered':
144
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED);
145
                break;
146
147
            case 'Unavailable':
148
                $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_READY);
149
                break;
150
151
            default:
152
                break;
153
        }
154
155
        return $recipient;
156
    }
157
}
158