1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\EventProjector\StoredEvent; |
8
|
|
|
use Spatie\EventProjector\EventProjectionist; |
9
|
|
|
use Spatie\EventProjector\Exceptions\InvalidEventHandler; |
10
|
|
|
|
11
|
|
|
class ReplayEventsCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $signature = 'event-projector:replay-events |
14
|
|
|
{--projector=*} : The projector that should receive the event'; |
15
|
|
|
|
16
|
|
|
protected $description = 'Replay stored events'; |
17
|
|
|
|
18
|
|
|
/** @var \Spatie\EventProjector\EventProjectionist */ |
19
|
|
|
protected $eventProjectionist; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $storedEventModelClass; |
23
|
|
|
|
24
|
|
|
public function __construct(EventProjectionist $eventProjectionist, string $storedEventModelClass) |
25
|
|
|
{ |
26
|
|
|
parent::__construct(); |
27
|
|
|
|
28
|
|
|
$this->eventProjectionist = $eventProjectionist; |
29
|
|
|
|
30
|
|
|
$this->storedEventModelClass = $storedEventModelClass; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function handle() |
34
|
|
|
{ |
35
|
|
|
if (! $this->commandShouldRun()) { |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$projectors = $this->getProjectors(); |
41
|
|
|
|
42
|
|
|
if ($projectors->isEmpty()) { |
43
|
|
|
$this->warn('No projectors found to replay events to...'); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->comment('Replaying events...'); |
49
|
|
|
|
50
|
|
|
$this->emptyLine(); |
51
|
|
|
|
52
|
|
|
$bar = $this->output->createProgressBar(StoredEvent::count()); |
53
|
|
|
|
54
|
|
|
$onEventReplayed = function () use ($bar) { |
55
|
|
|
$bar->advance(); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$this->eventProjectionist->replayEvents($projectors, $onEventReplayed); |
59
|
|
|
|
60
|
|
|
$bar->finish(); |
61
|
|
|
|
62
|
|
|
$this->emptyLine(2); |
63
|
|
|
|
64
|
|
|
$this->comment('All done!'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getProjectors(): Collection |
68
|
|
|
{ |
69
|
|
|
$onlyCallProjectors = $this->option('projector'); |
70
|
|
|
|
71
|
|
|
$this->guardAgainstNonExistingProjectors($onlyCallProjectors); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$allProjectors = $this->eventProjectionist->projectors; |
74
|
|
|
|
75
|
|
|
if (count($onlyCallProjectors) === 0) { |
76
|
|
|
return $allProjectors; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $allProjectors |
80
|
|
|
->filter(function ($projector) use ($onlyCallProjectors) { |
81
|
|
|
if (!is_string($projector)) { |
82
|
|
|
$projector = get_class($projector); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return in_array($projector, $onlyCallProjectors); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function guardAgainstNonExistingProjectors(array $onlyCallProjectors) |
90
|
|
|
{ |
91
|
|
|
foreach ($onlyCallProjectors as $projector) { |
92
|
|
|
if (!class_exists($projector)) { |
93
|
|
|
throw InvalidEventHandler::doesNotExist($projector); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function emptyLine(int $amount = 1) |
99
|
|
|
{ |
100
|
|
|
foreach (range(1, $amount) as $i) { |
101
|
|
|
$this->line(''); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function commandShouldRun(): bool |
106
|
|
|
{ |
107
|
|
|
if (count($this->option('projector') ?? []) === 0) { |
108
|
|
|
if (! $confirmed = $this->confirm('Are you sure you want to replay the events to all projectors?')) { |
109
|
|
|
$this->warn('No events replayed!'); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.