Completed
Push — develop ( 2e3dd3...676144 )
by Jens
05:16
created

AbstractApiResponse   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 21
c 2
b 1
f 1
lcom 1
cbo 5
dl 0
loc 181
ccs 46
cts 46
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toObject() 0 8 2
A toArray() 0 7 2
A getBody() 0 7 2
A isError() 0 6 1
A getResponseField() 0 5 2
A getErrors() 0 7 2
A getStatusCode() 0 4 1
A getHeader() 0 4 1
A getHeaders() 0 4 1
A getResponse() 0 4 1
A getRequest() 0 4 1
A wait() 0 8 2
A then() 0 9 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
    use ContextTrait;
24
25
    /**
26
     * @var ResponseInterface|AdapterPromiseInterface
27
     */
28
    protected $response;
29
30
    /**
31
     * @var ClientRequestInterface
32
     */
33
    protected $request;
34
35
    /**
36
     * @var array
37
     */
38
    protected $jsonData;
39
40
    /**
41
     * @var string
42
     */
43
    protected $responseBody;
44
45
    /**
46
     * @var ErrorContainer
47
     */
48
    private $errors;
49
50
    /**
51
     * @param ResponseInterface $response
52
     * @param ClientRequestInterface $request
53
     * @param Context $context
54
     */
55 326
    public function __construct(ResponseInterface $response, ClientRequestInterface $request, Context $context = null)
56
    {
57 326
        $this->setContext($context);
58 326
        $this->response = $response;
59 326
        $this->request = $request;
60 326
    }
61
62
    /**
63
     * @return mixed|null
64
     */
65 75
    public function toObject()
66
    {
67 75
        if (!$this->isError()) {
68 74
            return $this->getRequest()->mapResponse($this);
69
        }
70
71 1
        return null;
72
    }
73
74
    /**
75
     * @return array
76
     */
77 280
    public function toArray()
78
    {
79 280
        if (is_null($this->jsonData)) {
80 280
            $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...
81
        }
82 280
        return $this->jsonData;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 283
    public function getBody()
89
    {
90 283
        if (is_null($this->responseBody)) {
91 283
            $this->responseBody = (string)$this->response->getBody();
92
        }
93 283
        return $this->responseBody;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 261
    public function isError()
100
    {
101 261
        $statusCode = $this->getStatusCode();
102
103 261
        return (!in_array($statusCode, [200, 201]));
104
    }
105
106 13
    protected function getResponseField($fieldName, $default = '')
107
    {
108 13
        $result = $this->toArray();
109 13
        return isset($result[$fieldName]) ? $result[$fieldName]: $default;
110
    }
111
112
    /**
113
     * @return array
114
     */
115 13
    public function getErrors()
116
    {
117 13
        if (is_null($this->errors)) {
118 13
            $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...
119
        }
120 13
        return $this->errors;
121
    }
122
123 263
    public function getStatusCode()
124
    {
125 263
        return $this->getResponse()->getStatusCode();
126
    }
127
128
    /**
129
     * @param string $header
130
     * @return array
131
     */
132
    /**
133
     * @param string $header
134
     * @return string[]
135
     */
136 1
    public function getHeader($header)
137
    {
138 1
        return $this->getResponse()->getHeader($header);
139
    }
140
141
    /**
142
     * @return array
143
     */
144 1
    public function getHeaders()
145
    {
146 1
        return $this->getResponse()->getHeaders();
147
    }
148
149
    /**
150
     * @return ResponseInterface|AdapterPromiseInterface
151
     */
152 269
    public function getResponse()
153
    {
154 269
        return $this->response;
155
    }
156
157
    /**
158
     * @return ClientRequestInterface
159
     */
160 75
    public function getRequest()
161
    {
162 75
        return $this->request;
163
    }
164
165
    /**
166
     * Returns the result of the future either from cache or by blocking until
167
     * it is complete.
168
     *
169
     * This method must block until the future has a result or is cancelled.
170
     * Throwing an exception in the wait() method will mark the future as
171
     * realized and will throw the exception each time wait() is called.
172
     * Throwing an instance of GuzzleHttp\Ring\CancelledException will mark
173
     * the future as realized, will not throw immediately, but will throw the
174
     * exception if the future's wait() method is called again.
175
     *
176
     * @return mixed
177
     */
178 2
    public function wait()
179
    {
180 2
        if (!$this->getResponse() instanceof AdapterPromiseInterface) {
181 1
            throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL);
182
        }
183
184 1
        return $this->getResponse()->wait();
185
    }
186
187
    /**
188
     * @param callable $onFulfilled
189
     * @param callable $onRejected
190
     * @return ApiResponseInterface
191
     */
192 4
    public function then(callable $onFulfilled = null, callable $onRejected = null)
193
    {
194 4
        if (!$this->getResponse() instanceof AdapterPromiseInterface) {
195 1
            throw new \BadMethodCallException(Message::FUTURE_BAD_METHOD_CALL);
196
        }
197 3
        $this->getResponse()->then($onFulfilled, $onRejected);
198
199 3
        return $this;
200
    }
201
}
202