1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\EventProjector\Console\Concerns\ReplaysEvents; |
8
|
|
|
use Spatie\EventProjector\EventProjectionist; |
9
|
|
|
use Spatie\EventProjector\Models\StoredEvent; |
10
|
|
|
use Spatie\EventProjector\Projectors\Projector; |
11
|
|
|
use Spatie\EventProjector\Exceptions\InvalidEventHandler; |
12
|
|
|
|
13
|
|
|
class ReplayEventsCommand extends Command |
14
|
|
|
{ |
15
|
|
|
use ReplaysEvents; |
16
|
|
|
|
17
|
|
|
protected $signature = 'event-projector:replay-events |
18
|
|
|
{--projector=* : The projector that should receive the event}'; |
19
|
|
|
|
20
|
|
|
protected $description = 'Replay stored events'; |
21
|
|
|
|
22
|
|
|
/** @var \Spatie\EventProjector\EventProjectionist */ |
23
|
|
|
protected $eventProjectionist; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $storedEventModelClass; |
27
|
|
|
|
28
|
|
|
public function __construct(EventProjectionist $eventProjectionist, string $storedEventModelClass) |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
|
32
|
|
|
$this->eventProjectionist = $eventProjectionist; |
33
|
|
|
|
34
|
|
|
$this->storedEventModelClass = $storedEventModelClass; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function handle() |
38
|
|
|
{ |
39
|
|
|
if (! $this->commandShouldRun()) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$projectors = $this->getProjectors(); |
44
|
|
|
|
45
|
|
|
if ($projectors->isEmpty()) { |
46
|
|
|
$this->warn('No projectors found to replay events to...'); |
47
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->replayEvents($projectors); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getProjectors(): Collection |
55
|
|
|
{ |
56
|
|
|
$onlyCallProjectors = $this->option('projector'); |
57
|
|
|
|
58
|
|
|
$this->guardAgainstNonExistingProjectors($onlyCallProjectors); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$allProjectors = $this->eventProjectionist->getProjectors(); |
61
|
|
|
|
62
|
|
|
if (count($onlyCallProjectors) === 0) { |
63
|
|
|
return $allProjectors; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $allProjectors |
67
|
|
|
->filter(function ($projector) use ($onlyCallProjectors) { |
68
|
|
|
if (! is_string($projector)) { |
69
|
|
|
$projector = get_class($projector); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return in_array($projector, $onlyCallProjectors); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function guardAgainstNonExistingProjectors(array $onlyCallProjectors) |
77
|
|
|
{ |
78
|
|
|
foreach ($onlyCallProjectors as $projector) { |
79
|
|
|
if (! class_exists($projector)) { |
80
|
|
|
throw InvalidEventHandler::doesNotExist($projector); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function commandShouldRun(): bool |
86
|
|
|
{ |
87
|
|
|
if (count($this->option('projector') ?? []) === 0) { |
88
|
|
|
if (! $confirmed = $this->confirm('Are you sure you want to replay the events to all projectors?')) { |
89
|
|
|
$this->warn('No events replayed!'); |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.