Completed
Push — master ( 5838ba...64b2fe )
by Freek
01:53
created

ListCommand::handle()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
rs 8.5806
cc 4
eloc 19
nc 2
nop 0
1
<?php
2
3
namespace Spatie\EventProjector\Console;
4
5
use Illuminate\Console\Command;
6
use Spatie\EventProjector\EventProjectionist;
7
use Spatie\EventProjector\Projectors\Projector;
8
9
class ListCommand extends Command
10
{
11
    protected $signature = 'event-projector:list';
12
13
    protected $description = 'List all event projectors';
14
15
    /** @var \Spatie\EventProjector\EventProjectionist */
16
    protected $eventProjectionist;
17
18
    public function __construct(EventProjectionist $eventProjectionist)
19
    {
20
        parent::__construct();
21
22
        $this->eventProjectionist = $eventProjectionist;
23
    }
24
25
    public function handle()
26
    {
27
        $titles = ['Name', 'Up to date', 'Last processed event id', 'Last event processed at'];
28
29
        $projectors = $this->eventProjectionist->getProjectors();
30
31
        if ($projectors->isEmpty()) {
32
            $this->warn('No projectors found. You can register projector like this : `Spatie\EventProjector\Facades\EventProjectionist::addProjector($projectorClassName)`.');
33
34
            return;
35
        }
36
37
        $rows = $projectors
38
            ->map(function ($eventHandler) {
39
                if (is_string($eventHandler)) {
40
                    $eventHandler = app($eventHandler);
41
                }
42
43
                return $eventHandler;
44
            })
45
            ->map(function (Projector $projector) {
46
                return [
47
                    $projector->getName(),
48
                    $projector->hasReceivedAllEvents() ? '✅' : '❌',
49
                    $projector->getLastProcessedEventId(),
50
                    $projector->lastEventProcessedAt()->format('Y-m-d H:i:s'),
51
                ];
52
            })
53
            ->toArray();
54
55
        $this->table($titles, $rows);
56
    }
57
}
58