Completed
Push — master ( 2d0c9c...1268a4 )
by Jens
10:44
created

AbstractApiResponse::getResponseField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 26.01.15, 14:44
5
 */
6
7
namespace Commercetools\Core\Response;
8
9
use Commercetools\Core\Error\ErrorContainer;
10
use Psr\Http\Message\ResponseInterface;
11
use Commercetools\Core\Client\Adapter\AdapterPromiseInterface;
12
use Commercetools\Core\Error\Message;
13
use Commercetools\Core\Model\Common\Context;
14
use Commercetools\Core\Model\Common\ContextAwareInterface;
15
use Commercetools\Core\Model\Common\ContextTrait;
16
use Commercetools\Core\Request\ClientRequestInterface;
17
18
/**
19
 * @package Commercetools\Core\Response
20
 */
21
abstract class AbstractApiResponse implements ApiResponseInterface, ContextAwareInterface
22
{
23
    const X_CORRELATION_ID = 'X-Correlation-ID';
24
    use ContextTrait;
25
26
    /**
27
     * @var ResponseInterface|AdapterPromiseInterface
28
     */
29
    protected $response;
30
31
    /**
32
     * @var ClientRequestInterface
33
     */
34
    protected $request;
35
36
    /**
37
     * @var array
38
     */
39
    protected $jsonData;
40
41
    /**
42
     * @var string
43
     */
44
    protected $responseBody;
45
46
    /**
47
     * @var ErrorContainer
48
     */
49
    private $errors;
50
51
    /**
52
     * @param ResponseInterface $response
53
     * @param ClientRequestInterface $request
54
     * @param Context $context
55
     */
56 334
    public function __construct(ResponseInterface $response, ClientRequestInterface $request, Context $context = null)
57
    {
58 334
        $this->setContext($context);
59 334
        $this->response = $response;
60 334
        $this->request = $request;
61 334
    }
62
63
    /**
64
     * @return mixed|null
65
     */
66 75
    public function toObject()
67
    {
68 75
        if (!$this->isError()) {
69 74
            return $this->getRequest()->mapResponse($this);
70
        }
71
72 1
        return null;
73
    }
74
75
    /**
76
     * @return array
77
     */
78 284
    public function toArray()
79
    {
80 284
        if (is_null($this->jsonData)) {
81 284
            $this->jsonData = json_decode($this->getBody(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($this->getBody(), true) of type * is incompatible with the declared type array of property $jsonData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
        }
83 284
        return $this->jsonData;
84
    }
85
86
    /**
87
     * @return string
88
     */
89 287
    public function getBody()
90
    {
91 287
        if (is_null($this->responseBody)) {
92 287
            $this->responseBody = (string)$this->response->getBody();
93
        }
94 287
        return $this->responseBody;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100 266
    public function isError()
101
    {
102 266
        $statusCode = $this->getStatusCode();
103
104 266
        return (!in_array($statusCode, [200, 201]));
105
    }
106
107 15
    protected function getResponseField($fieldName, $default = '')
108
    {
109 15
        $result = $this->toArray();
110 15
        return isset($result[$fieldName]) ? $result[$fieldName]: $default;
111
    }
112
113
    /**
114
     * @return array
115
     */
116 15
    public function getErrors()
117
    {
118 15
        if (is_null($this->errors)) {
119 15
            $this->errors = ErrorContainer::fromArray($this->getResponseField('errors', []), $this->getContext());
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        }
121 15
        return $this->errors;
122
    }
123
124 1
    public function getCorrelationId()
125
    {
126 1
        return current($this->getHeader(self::X_CORRELATION_ID));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression current($this->getHeader...lf::X_CORRELATION_ID)); of type string|false adds false to the return on line 126 which is incompatible with the return type declared by the interface Commercetools\Core\Respo...rface::getCorrelationId of type string. It seems like you forgot to handle an error condition.
Loading history...
127
    }
128
129 268
    public function getStatusCode()
130
    {
131 268
        return $this->getResponse()->getStatusCode();
132
    }
133
134
    /**
135
     * @param string $header
136
     * @return array
137
     */
138
    /**
139
     * @param string $header
140
     * @return string[]
141
     */
142 2
    public function getHeader($header)
143
    {
144 2
        return $this->getResponse()->getHeader($header);
145
    }
146
147
    /**
148
     * @return array
149
     */
150 1
    public function getHeaders()
151
    {
152 1
        return $this->getResponse()->getHeaders();
153
    }
154
155
    /**
156
     * @return ResponseInterface|AdapterPromiseInterface
157
     */
158 275
    public function getResponse()
159
    {
160 275
        return $this->response;
161
    }
162
163
    /**
164
     * @return ClientRequestInterface
165
     */
166 75
    public function getRequest()
167
    {
168 75
        return $this->request;
169
    }
170
171
    /**
172
     * Returns the result of the future either from cache or by blocking until
173
     * it is complete.
174
     *
175
     * This method must block until the future has a result or is cancelled.
176
     * Throwing an exception in the wait() method will mark the future as
177
     * realized and will throw the exception each time wait() is called.
178
     * Throwing an instance of GuzzleHttp\Ring\CancelledException will mark
179
     * the future as realized, will not throw immediately, but will throw the
180
     * exception if the future's wait() method is called again.
181
     *
182
     * @return mixed
183
     */
184 2
    public function wait()
185
    {
186 2
        if (!$this->getResponse() instanceof AdapterPromiseInterface) {
187 1
            throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL);
188
        }
189
190 1
        return $this->getResponse()->wait();
191
    }
192
193
    /**
194
     * @param callable $onFulfilled
195
     * @param callable $onRejected
196
     * @return ApiResponseInterface
197
     */
198 4
    public function then(callable $onFulfilled = null, callable $onRejected = null)
199
    {
200 4
        if (!$this->getResponse() instanceof AdapterPromiseInterface) {
201 1
            throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL);
202
        }
203 3
        $this->getResponse()->then($onFulfilled, $onRejected);
204
205 3
        return $this;
206
    }
207
}
208