1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * Author: panosru |
||
7 | * Date: 27/04/2018 |
||
8 | * Time: 11:20 |
||
9 | */ |
||
10 | |||
11 | namespace Omega\FaultManager\Traits; |
||
12 | |||
13 | use Hoa\Event\Bucket; |
||
14 | use Hoa\Event\Event; |
||
15 | use Omega\FaultManager\Exceptions\EventHandlerAlreadyExists; |
||
16 | use Omega\FaultManager\Interfaces\FaultManagerEventHandler as IFaultManagerEventHandler; |
||
17 | |||
18 | /** |
||
19 | * Trait FaultEventStream |
||
20 | * @package Omega\FaultManager\Traits |
||
21 | */ |
||
22 | trait FaultEventStream |
||
23 | { |
||
24 | /** @var string */ |
||
25 | private static $eventStreamId = 'hoa://Event/Exception'; |
||
26 | |||
27 | /** @var bool */ |
||
28 | private static $eventStreamEnabled = false; |
||
29 | |||
30 | /** @var array */ |
||
31 | private static $handlers = []; |
||
32 | |||
33 | /** @var string */ |
||
34 | private static $eventStreamHandlerClosure; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public static function enableEventStream(): void |
||
40 | { |
||
41 | if (!self::isEventStreamEnabled()) { |
||
42 | self::$eventStreamEnabled = true; |
||
43 | |||
44 | if (!Event::getEvent(self::$eventStreamId)->isListened()) { |
||
45 | Event::getEvent(self::$eventStreamId)->attach(self::eventStreamHandler()); |
||
46 | } |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public static function disableEventStream(): void |
||
54 | { |
||
55 | if (self::isEventStreamEnabled()) { |
||
56 | self::$eventStreamEnabled = false; |
||
57 | |||
58 | if (Event::getEvent(self::$eventStreamId)->isListened()) { |
||
59 | Event::unregister(self::$eventStreamId, true); |
||
60 | self::$eventStreamHandlerClosure = null; |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public static function isEventStreamEnabled(): bool |
||
69 | { |
||
70 | return self::$eventStreamEnabled; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public static function registerHandler( |
||
77 | string $eventId, |
||
78 | IFaultManagerEventHandler $handler, |
||
79 | bool $override = false |
||
80 | ): void { |
||
81 | if (\array_key_exists($eventId, self::$handlers) && !$override) { |
||
82 | throw new EventHandlerAlreadyExists($eventId); |
||
83 | } |
||
84 | |||
85 | self::$handlers[$eventId] = $handler; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $eventId |
||
90 | */ |
||
91 | public static function unregisterHandler(string $eventId): void |
||
92 | { |
||
93 | if (isset(self::$handlers[$eventId])) { |
||
94 | unset(self::$handlers[$eventId]); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param \Hoa\Event\Source $router |
||
100 | * @param \Throwable $exception |
||
101 | * @throws \Hoa\Event\Exception |
||
102 | */ |
||
103 | protected static function registerEvent(\Hoa\Event\Source $router, \Throwable $exception): void |
||
104 | { |
||
105 | if (!Event::eventExists(self::$eventStreamId)) { |
||
106 | // @codeCoverageIgnoreStart |
||
107 | Event::register(self::$eventStreamId, $router); |
||
108 | // @codeCoverageIgnoreEnd |
||
109 | } |
||
110 | |||
111 | Event::notify( |
||
112 | self::$eventStreamId, |
||
113 | $router, |
||
114 | new Bucket($exception) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return \Closure |
||
120 | */ |
||
121 | private static function eventStreamHandler(): \Closure |
||
122 | { |
||
123 | if (null === self::$eventStreamHandlerClosure) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
124 | $handlers = &self::$handlers; |
||
125 | |||
126 | self::$eventStreamHandlerClosure = function (Bucket $bucket) use (&$handlers) { |
||
127 | /** @var \Hoa\Exception\Exception $exception */ |
||
128 | $exception = $bucket->getData(); |
||
129 | |||
130 | // Execute handler for the particular exception |
||
131 | if (isset($handlers[\get_class($exception)])) { |
||
132 | $handlers[\get_class($exception)]($exception); |
||
133 | } |
||
134 | |||
135 | // Execute global handler if exists |
||
136 | if (isset($handlers[\Omega\FaultManager\Fault::ALL_EVENTS])) { |
||
137 | $handlers[\Omega\FaultManager\Fault::ALL_EVENTS]($exception); |
||
138 | } |
||
139 | }; |
||
140 | } |
||
141 | |||
142 | return self::$eventStreamHandlerClosure; |
||
143 | } |
||
144 | } |
||
145 |