Completed
Push — master ( ed0a74...0ead96 )
by Tomáš
03:27
created

AbstractException::getMessageWithErrors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Inspirum\Balikobot\Exceptions;
4
5
use Inspirum\Balikobot\Contracts\ExceptionInterface;
6
use Inspirum\Balikobot\Definitions\Response;
7
use RuntimeException;
8
use Throwable;
9
10
abstract class AbstractException extends RuntimeException implements ExceptionInterface
11
{
12
    /**
13
     * Response data
14
     *
15
     * @var array<mixed>
16
     */
17
    protected $response;
18
19
    /**
20
     * Response HTTP status code
21
     *
22
     * @var int
23
     */
24
    protected $statusCode;
25
26
    /**
27
     * API response errors
28
     *
29
     * @var array<array<string,string>>
30
     */
31
    protected $errors = [];
32
33
    /**
34
     * AbstractException constructor
35
     *
36
     * @param array<mixed>    $response
37
     * @param int             $statusCode
38
     * @param \Throwable|null $previous
39
     * @param string|null     $message
40
     */
41 123
    public function __construct(
42
        array $response = [],
43
        int $statusCode = 500,
44
        Throwable $previous = null,
45
        string $message = null
46
    ) {
47
        // set response data
48 123
        $this->response = $response;
49
50
        // overwrite default HTTP status code
51 123
        $this->statusCode = $statusCode;
52
53
        // overwrite default message
54 123
        if ($message === null) {
55 123
            $message = Response::$statusCodesErrors[$statusCode] ?? 'Operace neproběhla v pořádku.';
56 123
            $message = $this->getMessageWithErrors($message);
57
        }
58
59
        // construct exception
60 123
        parent::__construct($message, $statusCode, $previous);
61 123
    }
62
63
    /**
64
     * Get response HTTP status code
65
     *
66
     * @return int
67
     */
68 7
    public function getStatusCode(): int
69
    {
70 7
        return $this->statusCode;
71
    }
72
73
    /**
74
     * Get response as array
75
     *
76
     * @return array<mixed>
77
     */
78 1
    public function getResponse(): array
79
    {
80 1
        return $this->response;
81
    }
82
83
    /**
84
     * Get response as string
85
     *
86
     * @return string
87
     */
88 1
    public function getResponseAsString(): string
89
    {
90 1
        return (string) json_encode($this->response);
91
    }
92
93
    /**
94
     * Get response errors
95
     *
96
     * @return array<array<string,string>>
97
     */
98 123
    public function getErrors(): array
99
    {
100 123
        return $this->errors;
101
    }
102
103
    /**
104
     * Get message with errors
105
     *
106
     * @param string $message
107
     *
108
     * @return string
109
     */
110 123
    private function getMessageWithErrors(string $message): string
111
    {
112 123
        $messages = [$message];
113
114 123
        foreach ($this->getErrors() as $i => $errors) {
115 18
            foreach ($errors as $key => $error) {
116 18
                $messages[] = sprintf('[%s][%s]: %s', $i, $key, $error);
117
            }
118
        }
119
120 123
        return implode("\n", $messages);
121
    }
122
}
123