Passed
Push — master ( b674ac...2bc77e )
by Kirill
04:21
created

AttributeException::fromDoctrine()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 1
dl 0
loc 19
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Exception;
13
14
15
use Doctrine\Common\Annotations\AnnotationException;
16
17
class AttributeException extends \RuntimeException
18
{
19
    /**
20
     * @var int
21
     */
22
    public const ERROR_CODE_SYNTAX = 0x01;
23
24
    /**
25
     * @var string
26
     */
27
    private const ERROR_PREFIX_SYNTAX = '[Syntax Error]';
28
29
    /**
30
     * @var int
31
     */
32
    public const ERROR_CODE_SEMANTIC = 0x02;
33
34
    /**
35
     * @var string
36
     */
37
    private const ERROR_PREFIX_SEMANTIC = '[Semantical Error]';
38
39
    /**
40
     * @var int
41
     */
42
    public const ERROR_CODE_CREATION = 0x03;
43
44
    /**
45
     * @var string
46
     */
47
    private const ERROR_PREFIX_CREATION = '[Creation Error]';
48
49
    /**
50
     * @var int
51
     */
52
    public const ERROR_CODE_TYPE = 0x04;
53
54
    /**
55
     * @var string
56
     */
57
    private const ERROR_PREFIX_TYPE = '[Type Error]';
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    final public function __construct(string $message = '', int $code = 0, \Throwable $previous = null)
63
    {
64
        parent::__construct($message, $code, $previous);
65
    }
66
67
    /**
68
     * Creates a new {@see AttributeException} describing a syntax error.
69
     *
70
     * @see AnnotationException::syntaxError()
71
     *
72
     * @param string $message Exception message
73
     * @return static
74
     */
75
    public static function syntaxError(string $message): self
76
    {
77
        return new static(self::ERROR_PREFIX_SYNTAX . ' ' . $message, static::ERROR_CODE_SYNTAX);
78
    }
79
80
    /**
81
     * Creates a new {@see AttributeException} describing a semantical error.
82
     *
83
     * @see AnnotationException::semanticalError()
84
     *
85
     * @param string $message Exception message
86
     * @return static
87
     */
88
    public static function semanticalError(string $message): self
89
    {
90
        return new static(self::ERROR_PREFIX_SEMANTIC . ' ' . $message, static::ERROR_CODE_SEMANTIC);
91
    }
92
93
    /**
94
     * Creates a new {@see AttributeException} describing an error which
95
     * occurred during the creation of the attribute.
96
     *
97
     * @see AnnotationException::creationError()
98
     *
99
     * @param string $message
100
     * @return static
101
     */
102
    public static function creationError(string $message): self
103
    {
104
        return new static(self::ERROR_PREFIX_CREATION . ' ' . $message, static::ERROR_CODE_CREATION);
105
    }
106
107
    /**
108
     * Creates a new {@see AttributeException} describing a type error.
109
     *
110
     * @see AnnotationException::typeError()
111
     *
112
     * @param string $message
113
     * @return static
114
     */
115
    public static function typeError(string $message): self
116
    {
117
        return new static(self::ERROR_PREFIX_TYPE . ' ' . $message, self::ERROR_CODE_TYPE);
118
    }
119
120
    /**
121
     * @param AnnotationException $e
122
     * @return static
123
     */
124
    public static function fromDoctrine(AnnotationException $e): self
125
    {
126
        [$message, $previous] = [$e->getMessage(), $e->getPrevious()];
127
128
        switch (true) {
129
            case \str_starts_with(self::ERROR_PREFIX_SYNTAX, $message):
130
                return new static($message, self::ERROR_CODE_SYNTAX, $previous);
131
132
            case \str_starts_with(self::ERROR_PREFIX_CREATION, $message):
133
                return new static($message, self::ERROR_CODE_CREATION, $previous);
134
135
            case \str_starts_with(self::ERROR_PREFIX_SEMANTIC, $message):
136
                return new static($message, self::ERROR_CODE_SEMANTIC, $previous);
137
138
            case \str_starts_with(self::ERROR_PREFIX_TYPE, $message):
139
                return new static($message, self::ERROR_CODE_TYPE, $previous);
140
141
            default:
142
                return new static($message, 0, $previous);
143
        }
144
    }
145
}
146