Issues (30)

src/Model/AbstractResponseModel.php (14 issues)

1
<?php
2
3
namespace MediaMonks\RestApi\Model;
4
5
use MediaMonks\RestApi\Exception\AbstractValidationException;
6
use MediaMonks\RestApi\Response\ExtendedResponseInterface;
7
use MediaMonks\RestApi\Response\PaginatedResponseInterface;
8
use MediaMonks\RestApi\Util\StringUtil;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
12
abstract class AbstractResponseModel
13
{
14
    protected int $statusCode = Response::HTTP_OK;
15
16
    protected bool $returnStatusCode = false;
17
18
    protected bool $returnStackTrace = false;
19
20
    protected mixed $data = null;
21
22
    protected ?Response $response = null;
23
24
    protected ?ExtendedResponseInterface $extendedResponse = null;
25
26
    protected ?\Throwable $throwable = null;
27
28
    protected ?PaginatedResponseInterface $pagination = null;
29
30
    public function isReturnStackTrace(): bool
31
    {
32
        return $this->returnStackTrace;
33
    }
34
35
    public function setReturnStackTrace(bool $returnStackTrace): ResponseModelInterface
36
    {
37
        $this->returnStackTrace = $returnStackTrace;
38
39
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
40
    }
41
42
    public function getStatusCode(): int
43
    {
44
        if (isset($this->throwable)) {
45
            return $this->getExceptionStatusCode();
46
        }
47
48
        if ($this->isEmpty()) {
49
            return Response::HTTP_NO_CONTENT;
50
        }
51 5
52
        return $this->statusCode;
53 5
    }
54
55
    protected function getExceptionStatusCode(): int
56
    {
57
        if ($this->throwable instanceof HttpException) {
58
            return $this->throwable->getStatusCode();
0 ignored issues
show
The method getStatusCode() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            return $this->throwable->/** @scrutinizer ignore-call */ getStatusCode();
Loading history...
The method getStatusCode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            return $this->throwable->/** @scrutinizer ignore-call */ getStatusCode();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        }
60 3
        if ($this->throwable instanceof AbstractValidationException) {
61
            return Response::HTTP_BAD_REQUEST;
62 3
        }
63
        if ($this->isValidHttpStatusCode($this->throwable->getCode())) {
64 3
            return $this->throwable->getCode();
65
        }
66
67
        return Response::HTTP_INTERNAL_SERVER_ERROR;
68
    }
69
70 14
    protected function isValidHttpStatusCode(int $code): bool
71
    {
72 14
        return array_key_exists($code, Response::$statusTexts) && $code >= Response::HTTP_BAD_REQUEST;
73 4
    }
74
75 10
    protected function getThrowableErrorCode(string $errorCode, ?string $trim = null): string
76 2
    {
77
        return sprintf($errorCode, StringUtil::classToSnakeCase($this->throwable, $trim));
78
    }
79 8
80
    protected function getThrowableStackTrace(): array
81
    {
82
        $traces = [];
83
        foreach ($this->throwable->getTrace() as $trace) {
84
            // Since PHP 7.4 the args key got disabled, to enable it again:
85 4
            // zend.exception_ignore_args = On
86
            if (array_key_exists('args', $trace)) {
87 4
                $trace['args'] = json_decode(json_encode($trace['args']), true);
88 1
            }
89
90 3
            $traces[] = $trace;
91 1
        }
92
93 2
        return $traces;
94 1
    }
95
96
    public function setStatusCode(int $statusCode): ResponseModelInterface
97 1
    {
98
        $this->statusCode = $statusCode;
99
100
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
101
    }
102
103
    public function getReturnStatusCode(): bool
104 2
    {
105
        return $this->returnStatusCode;
106 2
    }
107
108
    public function setReturnStatusCode(bool $returnStatusCode): ResponseModelInterface
109
    {
110
        $this->returnStatusCode = $returnStatusCode;
111
112
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
113
    }
114 4
115
    public function getData(): mixed
116 4
    {
117
        return $this->data;
118
    }
119
120
    public function setData(mixed $data): ResponseModelInterface
121
    {
122 1
        $this->data = $data;
123
124 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
125 1
    }
126 1
127 1
    public function getThrowable(): ?\Throwable
128 1
    {
129
        return $this->throwable;
130 1
    }
131
132
    public function setThrowable(\Throwable $throwable): ResponseModelInterface
133
    {
134
        $this->throwable = $throwable;
135
136
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
137 6
    }
138
139 6
    public function getPagination(): ?PaginatedResponseInterface
140
    {
141 6
        return $this->pagination;
142
    }
143
144
    public function setPagination(PaginatedResponseInterface $pagination): ResponseModelInterface
145
    {
146
        $this->pagination = $pagination;
147 10
        $this->setData($pagination->getData());
148
149 10
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
150
    }
151
152
    public function getResponse(): ?Response
153
    {
154
        return $this->response;
155
    }
156 3
157
    public function setResponse(Response $response): ResponseModelInterface
158 3
    {
159
        $this->response = $response;
160 3
        $this->setStatusCode($response->getStatusCode());
161
        $this->setData($response->getContent());
162
163
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
164
    }
165
166 10
    /**
167
     * @return ExtendedResponseInterface
168 10
     */
169
    public function getExtendedResponse()
170
    {
171
        return $this->extendedResponse;
172
    }
173
174
    /**
175 12
     * @param ExtendedResponseInterface $response
176
     * @return $this
177 12
     */
178
    public function setExtendedResponse(ExtendedResponseInterface $response): ResponseModelInterface
179 12
    {
180
        $this->extendedResponse = $response;
181
        $this->setStatusCode($response->getStatusCode());
0 ignored issues
show
The method getStatusCode() does not exist on MediaMonks\RestApi\Respo...tendedResponseInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MediaMonks\RestApi\Respo...tendedResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
        $this->setStatusCode($response->/** @scrutinizer ignore-call */ getStatusCode());
Loading history...
182
        $this->setData($response->getCustomContent());
183
184
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type MediaMonks\RestApi\Model\AbstractResponseModel which is incompatible with the type-hinted return MediaMonks\RestApi\Model\ResponseModelInterface.
Loading history...
185 11
    }
186
187 11
    public function isEmpty(): bool
188
    {
189
        return (
190
            !isset($this->throwable)
191
            && is_null($this->data)
192
            && !isset($this->pagination)
193
            && $this->isEmptyResponse()
194 8
            && $this->isEmptyExtendedResponse()
195
        );
196 8
    }
197
198 8
    protected function isEmptyResponse(): bool
199
    {
200
        return (!isset($this->response) || $this->response->isEmpty());
0 ignored issues
show
The method isEmpty() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
        return (!isset($this->response) || $this->response->/** @scrutinizer ignore-call */ isEmpty());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
    }
202
203
    protected function isEmptyExtendedResponse(): bool
204 11
    {
205
        return (!isset($this->extendedResponse) || $this->extendedResponse->isEmpty());
0 ignored issues
show
The method isEmpty() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
        return (!isset($this->extendedResponse) || $this->extendedResponse->/** @scrutinizer ignore-call */ isEmpty());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method isEmpty() does not exist on MediaMonks\RestApi\Respo...tendedResponseInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to MediaMonks\RestApi\Respo...tendedResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
        return (!isset($this->extendedResponse) || $this->extendedResponse->/** @scrutinizer ignore-call */ isEmpty());
Loading history...
206 11
    }
207
208
    // @codeCoverageIgnoreStart
209
210
    /**
211
     * This is called when an exception is thrown during the response transformation
212
     */
213 2
    public function __toString(): string
214
    {
215 2
        return json_encode(get_object_vars($this));
216 2
    }
217
    // @codeCoverageIgnoreEnd
218
}
219