Completed
Push — master ( bf244d...faabf5 )
by Freek
03:53 queued 02:28
created

ListCommand::convertEventHandlersToTableRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Spatie\EventProjector\EventHandlers\EventHandler;
8
use Spatie\EventProjector\Projectionist;
9
use Spatie\EventProjector\Projectors\Projector;
10
11
final class ListCommand extends Command
12
{
13
    protected $signature = 'event-projector:list';
14
15
    protected $description = 'Lists all event handlers';
16
17
    public function handle(Projectionist $projectionist)
18
    {
19
        $this->info('');
20
        $projectors = $projectionist->getProjectors();
21
        $rows = $this->convertEventHandlersToTableRows($projectors);
22
        count($rows)
23
            ? $this->table(['Event', 'Handled by projectors'], $rows)
24
            : 'No projectors registered';
25
26
        $this->info('');
27
        $projectors = $projectionist->getReactors();
28
        $rows = $this->convertEventHandlersToTableRows($projectors);
29
        count($rows)
30
            ? $this->table(['Event', 'Handled by reactors'], $rows)
31
            : 'No reactors registered';
32
    }
33
34
35
    private function convertEventHandlersToTableRows(Collection $eventHandlers): array
36
    {
37
        $events = $eventHandlers
38
            ->reduce(function ($events, EventHandler $eventHandler) {
39
                $eventHandler->getEventHandlingMethods()->each(function (string $method, string $eventClass) use (&$events, $eventHandler) {
40
                    $events[$eventClass][] = get_class($eventHandler);
41
                });
42
43
                return $events;
44
            }, []);
45
46
        return collect($events)->map(function (array $eventHandlers, string $eventClass) {
47
            return [$eventClass, implode(PHP_EOL, collect($eventHandlers)->sort()->toArray())];
48
        })
49
            ->sort()
50
            ->values()
51
            ->toArray();
52
    }
53
}
54
55