Completed
Push — master ( 00f04e...1d93d6 )
by Freek
03:46
created

ListCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\EventSourcing\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Spatie\EventSourcing\EventHandlers\EventHandler;
8
use Spatie\EventSourcing\Projectionist;
9
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())])
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting '('
Loading history...
45
            ->sort()
46
            ->values()
47
            ->toArray();
48
    }
49
}
50