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