ReadOnlyException   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 84
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A forAttribute() 0 22 2
1
<?php
2
/**
3
 * Incoming
4
 *
5
 * @author    Trevor Suarez (Rican7)
6
 * @copyright (c) Trevor Suarez
7
 * @link      https://github.com/Rican7/incoming
8
 * @license   MIT
9
 */
10
11
declare(strict_types=1);
12
13
namespace Incoming\Structure\Exception;
14
15
use BadMethodCallException;
16
use Throwable;
17
18
/**
19
 * An exception to be thrown when an attempt is made to modify a read-only
20
 * value, property, or state.
21
 */
22
class ReadOnlyException extends BadMethodCallException
23
{
24
25
    /**
26
     * Constants
27
     */
28
29
    /**
30
     * The default exception message.
31
     *
32
     * @var string
33
     */
34
    const DEFAULT_MESSAGE = 'Illegal modification attempt';
35
36
    /**
37
     * The exception code for an exception with an attribute context.
38
     *
39
     * @var int
40
     */
41
    const CODE_FOR_ATTRIBUTE = 1;
42
43
    /**
44
     * The message extension format for providing an attribute context's info.
45
     *
46
     * @var string
47
     */
48
    const MESSAGE_EXTENSION_FOR_ATTRIBUTE_FORMAT = ' for attribute `%s`';
49
50
    /**
51
     * The message extension format for providing an attribute's value info in
52
     * addition to the attribute's context.
53
     *
54
     * @var string
55
     */
56
    const MESSAGE_EXTENSION_FOR_ATTRIBUTE_WITH_VALUE_FORMAT = ' and value `%s`';
57
58
59
    /**
60
     * Properties
61
     */
62
63
    /**
64
     * The exception message.
65
     *
66
     * @var string
67
     */
68
    protected $message = self::DEFAULT_MESSAGE;
69
70
71
    /**
72
     * Methods
73
     */
74
75
    /**
76
     * Create an exception instance with an attribute's context.
77
     *
78
     * @param mixed $name The name of the attribute attempted to be modified.
79
     * @param mixed|null $value The value attempted to be set.
80
     * @param int $code The exception code.
81
     * @param Throwable|null $previous A previous exception used for chaining.
82
     * @return static The newly created exception.
83
     */
84 27
    public static function forAttribute(
85
        $name,
86
        $value = null,
87
        int $code = self::CODE_FOR_ATTRIBUTE,
88
        Throwable $previous = null
89
    ): self {
90 27
        $message = self::DEFAULT_MESSAGE;
91
92 27
        if (null !== $value) {
93 15
            $message .= sprintf(
94 15
                self::MESSAGE_EXTENSION_FOR_ATTRIBUTE_FORMAT . self::MESSAGE_EXTENSION_FOR_ATTRIBUTE_WITH_VALUE_FORMAT,
95 15
                $name,
96 15
                var_export($value, true)
97
            );
98
        } else {
99 12
            $message .= sprintf(
100 12
                self::MESSAGE_EXTENSION_FOR_ATTRIBUTE_FORMAT,
101 12
                $name
102
            );
103
        }
104
105 27
        return new static($message, $code, $previous);
106
    }
107
}
108