Completed
Push — master ( 452d21...8391b8 )
by Tomáš
09:19 queued 07:48
created

BadRequestException::setError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Inspirum\Balikobot\Exceptions;
4
5
use Inspirum\Balikobot\Definitions\Response;
6
use Throwable;
7
8
class BadRequestException extends AbstractException
9
{
10
    /**
11
     * BadRequestException constructor
12
     *
13
     * @param array<mixed>    $response
14
     * @param int             $statusCode
15
     * @param \Throwable|null $previous
16
     * @param string|null     $message
17
     */
18 120
    public function __construct(
19
        array $response,
20
        int $statusCode = 400,
21
        Throwable $previous = null,
22
        string $message = null
23
    ) {
24 120
        $this->setErrors($response);
25
26 120
        parent::__construct($response, $statusCode, $previous, $message);
27 120
    }
28
29
    /**
30
     * Set errors from response
31
     *
32
     * @param array<mixed> $response
33
     *
34
     * @return void
35
     */
36 120
    private function setErrors(array $response): void
37
    {
38 120
        $i = 0;
39
40
        // add errors from all packages
41 120
        while (isset($response[$i])) {
42
            // set errors for package
43 29
            if (is_array($response[$i])) {
44 29
                $this->setErrorsForPackage($i, $response[$i]);
45 2
            } elseif (is_numeric($response[$i]) && $response[$i] >= 400) {
46
                // get error message from code
47 1
                $error = $this->getErrorMessage('status', (int) $response[$i]);
48
49
                // set errors fro given package from response codes
50 1
                $this->setError($i, 'status', $error);
51
            } else {
52 1
                $this->setError($i, 'status', 'Nespecifikovaná chyba.');
53
            }
54
55
            // try next package
56 29
            $i++;
57
        }
58 120
    }
59
60
    /**
61
     * @param int          $number
62
     * @param array<mixed> $response
63
     *
64
     * @return void
65
     */
66 29
    private function setErrorsForPackage(int $number, array $response): void
67
    {
68
        // response does not have full errors
69 29
        if (isset($response['errors']) === false) {
70
            // try to resolve errors from codes
71 28
            $this->setErrorsFromResponseCodes($number, $response);
72
73 28
            return;
74
        }
75
76
        // set errors for given package
77 1
        foreach ($response['errors'] as $error) {
78 1
            $this->setError($number, $error['attribute'], $error['message']);
79
        }
80 1
    }
81
82
    /**
83
     * @param int                      $number
84
     * @param array<string,int|string> $response
85
     *
86
     * @return void
87
     */
88 28
    private function setErrorsFromResponseCodes(int $number, array $response)
89
    {
90 28
        foreach ($response as $key => $code) {
91
            // skip non-numeric codes
92 28
            if (is_numeric($code) === false || $code < 400) {
93 20
                continue;
94
            }
95
96
            // get error message from code
97 16
            $error = $this->getErrorMessage($key, (int) $code);
98
99
            // set errors fro given package from response codes
100 16
            $this->setError($number, $key, $error);
101
        }
102 28
    }
103
104
    /**
105
     * @param string $key
106
     * @param int    $code
107
     *
108
     * @return string
109
     */
110 16
    private function getErrorMessage(string $key, int $code): string
111
    {
112
        // get error message from code
113 16
        if ($key === 'status') {
114 6
            return Response::$statusCodesErrors[$code] ?? Response::$statusCodesErrors[500];
115
        }
116
117 12
        if (isset(Response::$packageDataKeyErrors[$code][$key])) {
118 5
            return Response::$packageDataKeyErrors[$code][$key];
119
        }
120
121 8
        return Response::$packageDataErrors[$code] ?? 'Nespecifikovaná chyba.';
122
    }
123
124
    /**
125
     * @param int    $number
126
     * @param string $key
127
     * @param string $error
128
     *
129
     * @return void
130
     */
131 17
    private function setError(int $number, string $key, string $error): void
132
    {
133 17
        $this->errors[$number][$key] = $error;
134 17
    }
135
}
136