Total Complexity | 9 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
14 | final class DeferredProvider implements ListenerProviderInterface |
||
15 | { |
||
16 | private EventDispatcherInterface $dispatcher; |
||
17 | private bool $deferEvents = false; |
||
18 | private array $events = []; |
||
19 | |||
20 | public function __construct(EventDispatcherInterface $dispatcher) |
||
21 | { |
||
22 | $this->dispatcher = $dispatcher; |
||
23 | } |
||
24 | |||
25 | public function getListenersForEvent(object $event): iterable |
||
26 | { |
||
27 | if ($this->deferEvents) { |
||
28 | yield fn($event) => $this->addEvent($event); |
||
29 | } else { |
||
30 | yield fn($event) => $this->dispatchEvent($event); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | public function deferEvents(): void |
||
35 | { |
||
36 | $this->deferEvents = true; |
||
37 | } |
||
38 | |||
39 | public function clearEvents(): void |
||
42 | } |
||
43 | |||
44 | public function dispatchEvents(): array |
||
45 | { |
||
46 | $events = $this->events; |
||
47 | $this->clearEvents(); |
||
48 | $this->deferEvents = false; |
||
49 | $dispatchedEvents = []; |
||
50 | |||
51 | foreach ($events as $event) { |
||
52 | $dispatchedEvents[] = $this->dispatchEvent($event); |
||
53 | } |
||
54 | |||
55 | return $dispatchedEvents; |
||
56 | } |
||
57 | |||
58 | private function addEvent(object $event): void |
||
59 | { |
||
60 | $this->events[] = $event; |
||
61 | } |
||
62 | |||
63 | private function dispatchEvent(object $event): object |
||
66 | } |
||
67 | } |
||
68 |