1 | <?php |
||
10 | final class ListCommand extends Command |
||
11 | { |
||
12 | protected $signature = 'event-sourcing:list'; |
||
13 | |||
14 | protected $description = 'Lists all event handlers'; |
||
15 | |||
16 | public function handle(Projectionist $projectionist) |
||
17 | { |
||
18 | $this->info(''); |
||
19 | $projectors = $projectionist->getProjectors(); |
||
20 | $rows = $this->convertEventHandlersToTableRows($projectors); |
||
21 | count($rows) |
||
22 | ? $this->table(['Event', 'Handled by projectors'], $rows) |
||
23 | : $this->warn('No projectors registered'); |
||
24 | |||
25 | $this->info(''); |
||
26 | $projectors = $projectionist->getReactors(); |
||
27 | $rows = $this->convertEventHandlersToTableRows($projectors); |
||
28 | count($rows) |
||
29 | ? $this->table(['Event', 'Handled by reactors'], $rows) |
||
30 | : $this->warn('No reactors registered'); |
||
31 | } |
||
32 | |||
33 | private function convertEventHandlersToTableRows(Collection $eventHandlers): array |
||
34 | { |
||
35 | $events = $eventHandlers |
||
36 | ->reduce(function ($events, EventHandler $eventHandler) { |
||
37 | $eventHandler->getEventHandlingMethods()->each(function (string $method, string $eventClass) use (&$events, $eventHandler) { |
||
38 | $events[$eventClass][] = get_class($eventHandler); |
||
39 | }); |
||
40 | |||
41 | return $events; |
||
42 | }, []); |
||
43 | |||
44 | return collect($events)->map(fn(array $eventHandlers, string $eventClass) => [$eventClass, implode(PHP_EOL, collect($eventHandlers)->sort()->toArray())]) |
||
|
|||
45 | ->sort() |
||
46 | ->values() |
||
47 | ->toArray(); |
||
48 | } |
||
49 | } |
||
50 |