Passed
Branch master (0d8fc3)
by Tomáš
12:26
created

BaseException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 111
ccs 21
cts 21
cp 1
rs 10
wmc 9

6 Methods

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