Issues (30)

src/Model/ResponseModel.php (4 issues)

1
<?php
2
3
namespace MediaMonks\RestApi\Model;
4
5
use JsonSerializable;
6
use MediaMonks\RestApi\Exception\ExceptionInterface;
7
use MediaMonks\RestApi\Response\Error;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
class ResponseModel extends AbstractResponseModel implements ResponseModelInterface
12
{
13
14
    const EXCEPTION_GENERAL = 'Exception';
15
16
    const EXCEPTION_HTTP = 'HttpException';
17
18 9
    public function toArray(): array
19
    {
20 9
        $return = [];
21 9
        if ($this->getReturnStatusCode()) {
22 2
            $return['statusCode'] = $this->getStatusCode();
23 2
        }
24 9
25 5
        if (isset($this->throwable)) {
26 9
            $return['error'] = $this->throwableToArray();
27 1
        } elseif (isset($this->response) && $this->response instanceof RedirectResponse) {
28 1
            $return['location'] = $this->response->headers->get('Location');
29 3
        } else {
30
            $return += $this->dataToArray();
31
        }
32 9
33
        return $return;
34
    }
35
36
    protected function dataToArray(): array
37
    {
38 3
        $return = [];
39
        if (isset($this->data)) {
40 3
            $return['data'] = $this->data;
41 3
            if (isset($this->pagination)) {
42 2
                $return['pagination'] = $this->pagination->toArray();
0 ignored issues
show
The method toArray() 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

42
                /** @scrutinizer ignore-call */ 
43
                $return['pagination'] = $this->pagination->toArray();

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...
43 2
            }
44 1
        }
45 1
46 2
        return $return;
47
    }
48 3
49
    protected function throwableToArray(): array
50
    {
51
        if ($this->throwable instanceof ExceptionInterface) {
52
            $error = $this->throwable->toArray();
0 ignored issues
show
The method toArray() 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

52
            /** @scrutinizer ignore-call */ 
53
            $error = $this->throwable->toArray();

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 toArray() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as MediaMonks\RestApi\Excep...ractValidationException. ( Ignorable by Annotation )

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

52
            /** @scrutinizer ignore-call */ 
53
            $error = $this->throwable->toArray();
Loading history...
53
        } elseif ($this->throwable instanceof HttpException) {
54 5
            $error = $this->httpExceptionToArray();
55
        } elseif ($this->throwable instanceof JsonSerializable) {
56 5
            $error = $this->throwable->jsonSerialize();
0 ignored issues
show
The method jsonSerialize() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as MediaMonks\RestApi\Tests...onSerializableException. ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $error = $this->throwable->jsonSerialize();
Loading history...
57 1
        } else {
58 5
            $error = $this->generalThrowableToArray();
59 1
        }
60 1
61 3
        if ($this->isReturnStackTrace()) {
62
            $error['stack_trace'] = $this->getThrowableStackTrace();
63 5
        }
64 1
65 1
        return $error;
66
    }
67 5
68
    protected function httpExceptionToArray(): array
69
    {
70
        return [
71
            'code' => $this->getThrowableErrorCode(
72
                Error::ERROR_KEY_HTTP,
73 1
                self::EXCEPTION_HTTP
74
            ),
75
            'message' => $this->throwable->getMessage(),
76 1
        ];
77 1
    }
78 1
79
    protected function generalThrowableToArray(): array
80
    {
81
        return [
82
            'code' => trim(
83
                $this->getThrowableErrorCode(
84 3
                    Error::ERROR_KEY_GENERAL,
85
                    self::EXCEPTION_GENERAL
86
                ),
87 3
                '.'
88 3
            ),
89 3
            'message' => $this->throwable->getMessage(),
90
        ];
91
    }
92
}
93