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

ResponseModel::generalThrowableToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MediaMonks\RestApi\Model;
4
5
use MediaMonks\RestApi\Exception\ExceptionInterface;
6
use MediaMonks\RestApi\Response\Error;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
10
class ResponseModel extends AbstractResponseModel
11
    implements ResponseModelInterface
12
{
13
14
    const EXCEPTION_GENERAL = 'Exception';
15
16
    const EXCEPTION_HTTP = 'HttpException';
17
18 9
    /**
19
     * @return array
20 9
     */
21 9
    public function toArray()
22 2
    {
23 2
        $return = [];
24 9
        if ($this->getReturnStatusCode()) {
25 5
            $return['statusCode'] = $this->getStatusCode();
26 9
        }
27 1
        if (isset($this->throwable)) {
28 1
            $return['error'] = $this->throwableToArray();
29 3
        } elseif (isset($this->response)
30
            && $this->response instanceof RedirectResponse
31
        ) {
32 9
            $return['location'] = $this->response->headers->get('Location');
33
        } else {
34
            $return += $this->dataToArray();
35
        }
36
37
        return $return;
38 3
    }
39
40 3
    /**
41 3
     * @return array
42 2
     */
43 2
    protected function dataToArray()
44 1
    {
45 1
        $return = [];
46 2
        if (isset($this->data)) {
47
            $return['data'] = $this->data;
48 3
            if (isset($this->pagination)) {
49
                $return['pagination'] = $this->pagination->toArray();
50
            }
51
        }
52
53
        return $return;
54 5
    }
55
56 5
    /**
57 1
     * @return array
58 5
     */
59 1
    protected function throwableToArray()
60 1
    {
61 3
        if ($this->throwable instanceof ExceptionInterface) {
62
            $error = $this->throwable->toArray();
63 5
        } elseif ($this->throwable instanceof HttpException) {
64 1
            $error = $this->httpExceptionToArray();
65 1
        } else {
66
            $error = $this->generalThrowableToArray();
67 5
        }
68
        if ($this->isReturnStackTrace()) {
69
            $error['stack_trace'] = $this->getThrowableStackTrace();
70
        }
71
72
        return $error;
73 1
    }
74
75
    /**
76 1
     * @return array
77 1
     */
78 1
    protected function httpExceptionToArray()
79
    {
80
        return [
81
            'code' => $this->getThrowableErrorCode(
82
                Error::CODE_HTTP,
83
                self::EXCEPTION_HTTP
84 3
            ),
85
            'message' => $this->throwable->getMessage(),
86
        ];
87 3
    }
88 3
89 3
    /**
90
     * @return array
91
     */
92
    protected function generalThrowableToArray()
93
    {
94
        return [
95
            'code' => trim(
96
                $this->getThrowableErrorCode(
97
                    Error::CODE_GENERAL,
98
                    self::EXCEPTION_GENERAL
99
                ),
100
                '.'
101
            ),
102
            'message' => $this->throwable->getMessage(),
103
        ];
104
    }
105
}
106