Response::getStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Response;
15
16
use DateTimeImmutable;
17
use DateTimeInterface;
18
use Graze\Gigya\Gigya;
19
use Illuminate\Support\Collection;
20
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface;
21
22
class Response implements ResponseInterface
23
{
24
    /**
25
     * @var object
26
     */
27
    protected $body;
28
29
    /**
30
     * @var int
31
     */
32
    protected $errorCode;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $errorMessage;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $errorDetails;
43
44
    /**
45
     * @var int
46
     */
47
    protected $statusCode;
48
49
    /**
50
     * @var string
51
     */
52
    protected $statusReason;
53
54
    /**
55
     * @var string
56
     */
57
    protected $callId;
58
59
    /**
60
     * @var DateTimeImmutable
61
     */
62
    protected $time;
63
64
    /**
65
     * @var Collection
66
     */
67
    protected $data;
68
69
    /**
70
     * @var GuzzleResponseInterface
71
     */
72
    protected $response;
73
74
    /**
75
     * @param GuzzleResponseInterface $response
76
     */
77 13
    public function __construct(GuzzleResponseInterface $response)
78
    {
79 13
        $this->response = $response;
80 13
        $this->body = json_decode($response->getBody());
81 13
        $this->errorCode = (int) $this->popField('errorCode');
82 13
        $this->errorMessage = $this->popField('errorMessage');
83 13
        $this->errorDetails = $this->popField('errorDetails');
84 13
        $this->statusCode = (int) $this->popField('statusCode');
85 13
        $this->statusReason = $this->popField('statusReason');
86 13
        $this->callId = $this->popField('callId');
87 13
        $this->time = DateTimeImmutable::createFromFormat(Gigya::DATE_TIME_FORMAT, $this->popField('time'));
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTimeImmutable::creat...this->popField('time')) can also be of type false. However, the property $time is declared as type DateTimeImmutable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88 13
    }
89
90
    /**
91
     * Get a field from the body if it exists, and remove the name from the array.
92
     *
93
     * @param string $name
94
     *
95
     * @return mixed|null The value or null if the field does not exist
96
     */
97 13
    public function popField($name)
98
    {
99 13
        if (property_exists($this->body, $name)) {
100 13
            $value = $this->body->{$name};
101 13
            unset($this->body->{$name});
102
103 13
            return $value;
104
        }
105
106 11
        return null;
107
    }
108
109
    /**
110
     * @return int
111
     */
112 11
    public function getErrorCode()
113
    {
114 11
        return $this->errorCode;
115
    }
116
117
    /**
118
     * @return string|null
119
     */
120 6
    public function getErrorMessage()
121
    {
122 6
        return $this->errorMessage;
123
    }
124
125
    /**
126
     * @return string|null
127
     */
128 6
    public function getErrorDetails()
129
    {
130 6
        return $this->errorDetails;
131
    }
132
133
    /**
134
     * @return int
135
     */
136 8
    public function getStatusCode()
137
    {
138 8
        return $this->statusCode;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 7
    public function getStatusReason()
145
    {
146 7
        return $this->statusReason;
147
    }
148
149
    /**
150
     * @return string
151
     */
152 1
    public function getCallId()
153
    {
154 1
        return $this->callId;
155
    }
156
157
    /**
158
     * @return DateTimeInterface
159
     */
160 1
    public function getTime()
161
    {
162 1
        return $this->time;
163
    }
164
165
    /**
166
     * @return Collection
167
     */
168 9
    public function getData()
169
    {
170 9
        if (!$this->data) {
171 9
            $this->data = new Collection($this->body);
172
        }
173
174 9
        return $this->data;
175
    }
176
177
    /**
178
     * @return GuzzleResponseInterface
179
     */
180 1
    public function getOriginalResponse()
181
    {
182 1
        return $this->response;
183
    }
184
185
    /**
186
     * @return string
187
     */
188 5
    public function __toString()
189
    {
190 5
        return sprintf(
191 5
            "Response: %d: %s - %d: %s\n%s\n%s\n%s",
192 5
            $this->getStatusCode(),
193 5
            $this->getStatusReason(),
194 5
            $this->getErrorCode(),
195 5
            ErrorCode::getName($this->getErrorCode()),
196 5
            ErrorCode::getDescription($this->getErrorCode()),
197 5
            $this->getErrorMessage(),
198 5
            $this->getErrorDetails()
199
        );
200
    }
201
}
202