Completed
Push — master ( 7f2c26...da397a )
by
unknown
05:31
created

AbstractResponseModel::getExceptionStatusCode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace MediaMonks\RestApi\Model;
4
5
use MediaMonks\RestApi\Exception\AbstractValidationException;
6
use MediaMonks\RestApi\Response\PaginatedResponseInterface;
7
use MediaMonks\RestApi\Util\StringUtil;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
abstract class AbstractResponseModel
12
{
13
    /**
14
     * @var int
15
     */
16
    protected $statusCode = Response::HTTP_OK;
17
18
    /**
19
     * @var bool
20
     */
21
    protected $returnStatusCode = false;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $returnStackTrace = false;
27
28
    /**
29
     * @var mixed
30
     */
31
    protected $data;
32
33
    /**
34
     * @var Response
35
     */
36
    protected $response;
37
38
    /**
39
     * @var \Throwable
40
     */
41
    protected $throwable;
42
43
    /**
44
     * @var PaginatedResponseInterface
45
     */
46
    protected $pagination;
47
48
    /**
49
     * @return boolean
50
     */
51 5
    public function isReturnStackTrace()
52
    {
53 5
        return $this->returnStackTrace;
54
    }
55
56
    /**
57
     * @param boolean $returnStackTrace
58
     * @return $this
59
     */
60 3
    public function setReturnStackTrace($returnStackTrace)
61
    {
62 3
        $this->returnStackTrace = $returnStackTrace;
63
64 3
        return $this;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 14
    public function getStatusCode()
71
    {
72 14
        if (isset($this->throwable)) {
73 4
            return $this->getExceptionStatusCode();
74
        }
75 10
        if ($this->isEmpty()) {
76 2
            return Response::HTTP_NO_CONTENT;
77
        }
78
79 8
        return $this->statusCode;
80
    }
81
82
    /**
83
     * @return int
84
     */
85 4
    protected function getExceptionStatusCode()
86
    {
87 4
        if ($this->throwable instanceof HttpException) {
88 1
            return $this->throwable->getStatusCode();
89
        }
90 3
        if ($this->throwable instanceof AbstractValidationException) {
91 1
            return Response::HTTP_BAD_REQUEST;
92
        }
93 2
        if ($this->isValidHttpStatusCode($this->throwable->getCode())) {
94 1
            return $this->throwable->getCode();
95
        }
96
97 1
        return Response::HTTP_INTERNAL_SERVER_ERROR;
98
    }
99
100
    /**
101
     * @param int $code
102
     * @return bool
103
     */
104 2
    protected function isValidHttpStatusCode($code)
105
    {
106 2
        return array_key_exists($code, Response::$statusTexts) && $code >= Response::HTTP_BAD_REQUEST;
107
    }
108
109
    /**
110
     * @param string $errorCode
111
     * @param string $trim
112
     * @return string
113
     */
114 4
    protected function getThrowableErrorCode($errorCode, $trim = null)
115
    {
116 4
        return sprintf($errorCode, StringUtil::classToSnakeCase($this->throwable, $trim));
117
    }
118
119
    /**
120
     * @return string
121
     */
122 1
    protected function getThrowableStackTrace()
123
    {
124 1
        $traces = [];
125 1
        foreach ($this->throwable->getTrace() as $trace) {
126 1
            $trace['args'] = json_decode(json_encode($trace['args']), true);
127 1
            $traces[] = $trace;
128 1
        }
129
130 1
        return $traces;
131
    }
132
133
    /**
134
     * @param int $statusCode
135
     * @return $this
136
     */
137 6
    public function setStatusCode($statusCode)
138
    {
139 6
        $this->statusCode = $statusCode;
140
141 6
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 10
    public function getReturnStatusCode()
148
    {
149 10
        return $this->returnStatusCode;
150
    }
151
152
    /**
153
     * @param bool $returnStatusCode
154
     * @return $this
155
     */
156 3
    public function setReturnStatusCode($returnStatusCode)
157
    {
158 3
        $this->returnStatusCode = $returnStatusCode;
159
160 3
        return $this;
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166 10
    public function getData()
167
    {
168 10
        return $this->data;
169
    }
170
171
    /**
172
     * @param mixed $data
173
     * @return $this
174
     */
175 12
    public function setData($data)
176
    {
177 12
        $this->data = $data;
178
179 12
        return $this;
180
    }
181
182
    /**
183
     * @return \Exception
184
     */
185 11
    public function getThrowable()
186
    {
187 11
        return $this->throwable;
188
    }
189
190
    /**
191
     * @param \Throwable $throwable
192
     *
193
     * @return $this
194 8
     */
195
    public function setThrowable(\Throwable $throwable)
196 8
    {
197
        $this->throwable = $throwable;
198 8
199
        return $this;
200
    }
201
202
    /**
203
     * @return PaginatedResponseInterface
204 11
     */
205
    public function getPagination()
206 11
    {
207
        return $this->pagination;
208
    }
209
210
    /**
211
     * @param PaginatedResponseInterface $pagination
212
     * @return $this
213 2
     */
214
    public function setPagination(PaginatedResponseInterface $pagination)
215 2
    {
216 2
        $this->pagination = $pagination;
217
        $this->setData($pagination->getData());
218 2
219
        return $this;
220
    }
221
222
    /**
223
     * @return Response
224 11
     */
225
    public function getResponse()
226 11
    {
227
        return $this->response;
228
    }
229
230
    /**
231
     * @param Response $response
232
     * @return $this
233 4
     */
234
    public function setResponse(Response $response)
235 4
    {
236 4
        $this->response = $response;
237 4
        $this->setStatusCode($response->getStatusCode());
238
        $this->setData($response->getContent());
239 4
240
        return $this;
241
    }
242
243
    /**
244
     * @return bool
245 13
     */
246
    public function isEmpty()
247
    {
248 13
        return (
249 13
            !isset($this->throwable)
250 13
            && is_null($this->data)
251 13
            && !isset($this->pagination)
252 13
            && $this->isEmptyResponse()
253
        );
254
    }
255
256
    /**
257
     * @return bool
258 2
     */
259
    protected function isEmptyResponse()
260 2
    {
261
        return !isset($this->response) || $this->response->isEmpty();
262
    }
263
264
    // @codeCoverageIgnoreStart
265
    /**
266
     * This is called when an exception is thrown during the response transformation
267
     *
268
     * @return string
269
     */
270
    public function __toString()
271
    {
272
        return json_encode(get_object_vars($this));
273
    }
274
    // @codeCoverageIgnoreEnd
275
}
276