Completed
Push — master ( 0512a1...7c8694 )
by Tomáš
03:46
created

BadRequestException::setErrorsFromResponseCodes()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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