Completed
Push — master ( fa1726...5762e2 )
by Freek
03:49 queued 01:51
created

ListCommand::getLastEventProcessedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector\Console;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Collection;
8
use Spatie\EventProjector\EventProjectionist;
9
use Spatie\EventProjector\Models\ProjectorStatus;
10
use Spatie\EventProjector\Projectors\Projector;
11
12
class ListCommand extends Command
13
{
14
    protected $signature = 'event-projector:list';
15
16
    protected $description = 'List all event projectors';
17
18
    /** @var \Spatie\EventProjector\EventProjectionist */
19
    protected $eventProjectionist;
20
21
    public function __construct(EventProjectionist $eventProjectionist)
22
    {
23
        parent::__construct();
24
25
        $this->eventProjectionist = $eventProjectionist;
26
    }
27
28
    public function handle()
29
    {
30
        $projectors = $this->eventProjectionist->getProjectors();
31
32
        if ($projectors->isEmpty()) {
33
            $this->warn('No projectors found. You can register projector like this : `Spatie\EventProjector\Facades\EventProjectionist::addProjector($projectorClassName)`.');
34
35
            return;
36
        }
37
38
        $this->listProjectorsWithMissingEvents();
39
40
        $this->list($projectors);
41
    }
42
43
    private function listProjectorsWithMissingEvents()
44
    {
45
        $header = ['Name', 'Last processed event id', 'Stream', 'Last event received at'];
46
47
        $rows = ProjectorStatus::query()
48
            ->where('has_received_all_events', false)
49
            ->get()
50
            ->map(function (ProjectorStatus $projectorStatus) {
51
                return [
52
                    $projectorStatus->getProjector()->getName(),
53
                    $projectorStatus->last_processed_event_id,
54
                    $projectorStatus->stream,
55
                    $projectorStatus->updated_at,
56
                ];
57
            })
58
            ->sortBy(function (array $projectorStatusRow) {
59
                return $projectorStatusRow[0];
60
            })
61
            ->toArray();
62
63
        if (count($rows)) {
64
            $this->title('Projectors that have not receveived all events yet');
65
            $this->table($header, $rows);
66
        }
67
    }
68
69
    protected function list(Collection $projectors): void
70
    {
71
        $this->title('All projectors');
72
73
        $header = ['Name', 'Last processed event id', 'Last event processed at'];
74
75
        $rows = $projectors
76
            ->map(function (Projector $projector) {
77
                return [
78
                    $projector->getName(),
79
                    $this->getLastProcessedEventId($projector),
80
                    optional($this->getLastEventProcessedAt($projector))->format('Y-m-d H:i:s') ?? '',
81
                ];
82
            })
83
            ->toArray();
84
85
        $this->table($header, $rows);
86
    }
87
88
    public function getLastProcessedEventId(Projector $projector): int
89
    {
90
        return ProjectorStatus::query()
91
                ->where('projector_name', $projector->getName())
92
                ->max('last_processed_event_id') ?? 0;
93
    }
94
95
    public function getLastEventProcessedAt(Projector $projector): ?Carbon
96
    {
97
        $status = ProjectorStatus::query()
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
            ->where('projector_name', $projector->getName())
99
            ->orderBy('updated_at', 'desc')
100
            ->first();
101
102
        return optional($status)->updated_at;
103
    }
104
105
    protected function title(string $title)
106
    {
107
        $this->warn('');
108
        $this->warn($title);
109
        $this->warn(str_repeat('-', strlen($title)));
110
    }
111
}
112