Lines 69.23% 18 / 26
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 listener 100.00% 5 / 5 100.00% 1 / 1 1
 hook 0.00% 0 / 8 0.00% 0 / 1 2
 rowHook 100.00% 8 / 8 100.00% 1 / 1 1
 __construct 100.00% 5 / 5 100.00% 1 / 1 1
12final class WriteVetoException extends \RuntimeException
13{
14    /**
15     * The class-string of the model whose write was vetoed.
16     *
17     * @var class-string<\BlueprintAU\Radiant\Model>
18     */
19    public readonly string $model;
20
21    /**
22     * The vetoing source: the lifecycle event name (`saving`, `deleting`,
23     * `restoring`) or the hook path a trait claimed (`insert`, `update`,
24     * `delete`).
25     *
26     * @var string
27     */
28    public readonly string $event;
29
30    /**
31     * The trait whose hook vetoed — null when a listener vetoed.
32     *
33     * @var class-string|null
34     */
35    public readonly ?string $trait;
36
37    /**
38     * The vetoing hook method's name — null when a listener vetoed.
39     *
40     * @var string|null
41     */
42    public readonly ?string $method;
43
44    /**
45     * Create the lifecycle-veto exception.
46     *
47     * @param  class-string<\BlueprintAU\Radiant\Model>  $modelClass
48     * @param  string  $event  The attempt event that carried the veto.
49     * @return self
50     */
51    public static function listener(string $modelClass, string $event): self
52    {
53        return new self(
54            "The `{$event}` listener vetoed the write on model [{$modelClass}].",
55            $modelClass,
56            $event,
57        );
58    }
59
60    /**
61     * Create the trait-hook veto exception.
62     *
63     * @param  class-string<\BlueprintAU\Radiant\Model>  $modelClass
64     * @param  string  $event  The hook path that carried the veto.
65     * @param  class-string  $trait
66     * @param  string  $method
67     * @return self
68     */
69    public static function hook(string $modelClass, string $event, string $trait, string $method): self
70    {
71        return new self(
72            "The #[WriteHook] method [{$trait}::{$method}] vetoed the {$event} "
73            . "on model [{$modelClass}].",
74            $modelClass,
75            $event,
76            $trait,
77            $method,
78        );
79    }
80
81    /**
82     * Create the bulk RowHook veto exception.
83     *
84     * @param  class-string<\BlueprintAU\Radiant\Model>  $modelClass
85     * @param  string  $event  The hook path that carried the veto.
86     * @param  class-string  $trait
87     * @param  string  $method
88     * @return self
89     */
90    public static function rowHook(string $modelClass, string $event, string $trait, string $method): self
91    {
92        return new self(
93            "The #[RowHook(Hook::{$event})] method [{$trait}::{$method}] vetoed the write "
94            . "on model [{$modelClass}].",
95            $modelClass,
96            $event,
97            $trait,
98            $method,
99        );
100    }
101
102    /**
103     * @param  string  $message
104     * @param  class-string<\BlueprintAU\Radiant\Model>  $modelClass
105     * @param  string  $event
106     * @param  class-string|null  $trait
107     * @param  string|null  $method
108     */
109    private function __construct(
110        string $message,
111        string $modelClass,
112        string $event,
113        ?string $trait = null,
114        ?string $method = null,
115    ) {
116        parent::__construct($message);
117
118        $this->model = $modelClass;
119        $this->event = $event;
120        $this->trait = $trait;
121        $this->method = $method;
122    }
123}