Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
OrmException | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
codeToMessage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | namespace Miniframe\ORM\Exception; |
4 | |
5 | class OrmException extends \Exception |
6 | { |
7 | public const |
8 | UNEXPECTED = 0, |
9 | DUPLICATE_KEY = 1 |
10 | ; |
11 | |
12 | /** |
13 | * ORM Exception |
14 | * |
15 | * @param integer $code One of the OrmException:: constants. |
16 | * @param null|string $message The Exception message. When null, a default based on the code will be used. |
17 | * @param null|\Throwable $previous The previous throwable used for the exception chaining. |
18 | */ |
19 | public function __construct(int $code = self::UNEXPECTED, string $message = null, \Throwable $previous = null) |
20 | { |
21 | parent::__construct($message ?? $this->codeToMessage($code), $code, $previous); |
22 | } |
23 | |
24 | /** |
25 | * Converts a code to a message. |
26 | * |
27 | * @param integer $code The code. |
28 | * |
29 | * @return string |
30 | */ |
31 | private function codeToMessage(int $code): string |
32 | { |
33 | switch ($code) { |
34 | case static::DUPLICATE_KEY: |
35 | return 'Duplicate entry for key'; |
36 | case static::UNEXPECTED: |
37 | default: |
38 | return 'Unexpected error'; |
39 | } |
40 | } |
41 | } |