Passed
Push — master ( 404cfa...4a2326 )
by Eugene
04:47 queued 02:50
created

Error::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client;
15
16
final class Error
17
{
18
    private $type;
19
    private $file;
20
    private $line;
21
    private $message;
22
    private $number;
23
    private $code;
24
    private $fields;
25
    private $previous;
26
27 81
    public function __construct(
28
        string $type,
29
        string $file,
30
        int $line,
31
        string $message,
32
        int $number,
33
        int $code,
34
        array $fields = [],
35
        ?self $previous = null
36
    ) {
37 81
        $this->type = $type;
38 81
        $this->file = $file;
39 81
        $this->line = $line;
40 81
        $this->message = $message;
41 81
        $this->number = $number;
42 81
        $this->code = $code;
43 81
        $this->fields = $fields;
44 81
        $this->previous = $previous;
45
    }
46
47 81
    public static function fromMap(array $errorMap) : self
48
    {
49 81
        if (empty($errorMap[Keys::ERROR_STACK])) {
50
            throw new \InvalidArgumentException('The error map should contain a non-empty error stack');
51
        }
52
53 81
        $errorStack = $errorMap[Keys::ERROR_STACK];
54
55 81
        $first = \count($errorStack) - 1;
56 81
        $error = new self(
57 81
            $errorStack[$first][Keys::ERROR_TYPE],
58 81
            $errorStack[$first][Keys::ERROR_FILE],
59 81
            $errorStack[$first][Keys::ERROR_LINE],
60 81
            $errorStack[$first][Keys::ERROR_MESSAGE],
61 81
            $errorStack[$first][Keys::ERROR_NUMBER],
62 81
            $errorStack[$first][Keys::ERROR_CODE],
63 81
            $errorStack[$first][Keys::ERROR_FIELDS] ?? []
64
        );
65
66 81
        if (0 === $first) {
67 75
            return $error;
68
        }
69
70 6
        for ($i = $first - 1; $i >= 0; --$i) {
71 6
            $error = new self(
72 6
                $errorStack[$i][Keys::ERROR_TYPE],
73 6
                $errorStack[$i][Keys::ERROR_FILE],
74 6
                $errorStack[$i][Keys::ERROR_LINE],
75 6
                $errorStack[$i][Keys::ERROR_MESSAGE],
76 6
                $errorStack[$i][Keys::ERROR_NUMBER],
77 6
                $errorStack[$i][Keys::ERROR_CODE],
78 6
                $errorStack[$i][Keys::ERROR_FIELDS] ?? [],
79
                $error
80
            );
81
        }
82
83 6
        return $error;
84
    }
85
86
    public function toMap() : array
87
    {
88
        $error = $this;
89
        $errorStack = [];
90
91
        do {
92
            $errorStack[] = [
93
                Keys::ERROR_TYPE => $error->type,
94
                Keys::ERROR_FILE => $error->file,
95
                Keys::ERROR_LINE => $error->line,
96
                Keys::ERROR_MESSAGE => $error->message,
97
                Keys::ERROR_NUMBER => $error->number,
98
                Keys::ERROR_CODE => $error->code,
99
                Keys::ERROR_FIELDS => $error->fields,
100
            ];
101
        } while ($error = $error->getPrevious());
102
103
        return [Keys::ERROR_STACK => $errorStack];
104
    }
105
106 6
    public function getType() : string
107
    {
108 6
        return $this->type;
109
    }
110
111
    public function getFile() : string
112
    {
113
        return $this->file;
114
    }
115
116
    public function getLine() : int
117
    {
118
        return $this->line;
119
    }
120
121 15
    public function getMessage() : string
122
    {
123 15
        return $this->message;
124
    }
125
126
    public function getNumber() : int
127
    {
128
        return $this->number;
129
    }
130
131 15
    public function getCode() : int
132
    {
133 15
        return $this->code;
134
    }
135
136 6
    public function getFields() : array
137
    {
138 6
        return $this->fields;
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function getField(string $name)
145
    {
146
        if (\array_key_exists($name, $this->fields)) {
147
            return $this->fields[$name];
148
        }
149
150
        throw new \OutOfRangeException(\sprintf('The field "%s" does not exist', $name));
151
    }
152
153
    /**
154
     * @param mixed $default
155
     * @return mixed
156
     */
157 12
    public function tryGetField(string $name, $default = null)
158
    {
159 12
        return \array_key_exists($name, $this->fields) ? $this->fields[$name] : $default;
160
    }
161
162 9
    public function getPrevious() : ?self
163
    {
164 9
        return $this->previous;
165
    }
166
}
167