These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\EventProjector\Console; |
||
4 | |||
5 | use Exception; |
||
6 | use Illuminate\Console\Command; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use Spatie\EventProjector\Projectionist; |
||
9 | |||
10 | class ReplayCommand extends Command |
||
11 | { |
||
12 | protected $signature = 'event-projector:replay {projector?*} |
||
13 | {--from=0 : Replay events starting from this event number}'; |
||
14 | |||
15 | protected $description = 'Replay stored events'; |
||
16 | |||
17 | /** @var \Spatie\EventProjector\Projectionist */ |
||
18 | protected $projectionist; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $storedEventModelClass; |
||
22 | |||
23 | public function __construct(Projectionist $projectionist) |
||
24 | { |
||
25 | parent::__construct(); |
||
26 | |||
27 | $this->projectionist = $projectionist; |
||
28 | |||
29 | $this->storedEventModelClass = $this->getStoredEventClass(); |
||
30 | } |
||
31 | |||
32 | public function handle(): void |
||
33 | { |
||
34 | $projectors = $this->selectProjectors($this->argument('projector')); |
||
0 ignored issues
–
show
|
|||
35 | |||
36 | if (is_null($projectors)) { |
||
37 | $this->warn('No events replayed!'); |
||
38 | |||
39 | return; |
||
40 | } |
||
41 | |||
42 | $this->replay($projectors, $this->option('from')); |
||
43 | } |
||
44 | |||
45 | public function selectProjectors(array $projectorClassNames): ?Collection |
||
46 | { |
||
47 | if (count($projectorClassNames ?? []) === 0) { |
||
48 | if (! $confirmed = $this->confirm('Are you sure you want to replay events to all projectors?')) { |
||
49 | return null; |
||
50 | } |
||
51 | |||
52 | return $this->projectionist->getProjectors(); |
||
53 | } |
||
54 | |||
55 | return collect($projectorClassNames) |
||
56 | ->map(function (string $projectorName) { |
||
57 | return ltrim($projectorName, '\\'); |
||
58 | }) |
||
59 | ->map(function (string $projectorName) { |
||
60 | if (! $projector = $this->projectionist->getProjector($projectorName)) { |
||
61 | throw new Exception("Projector {$projectorName} not found. Did you register it?"); |
||
62 | } |
||
63 | |||
64 | return $projector; |
||
65 | }); |
||
66 | } |
||
67 | |||
68 | public function replay(Collection $projectors, int $startingFrom): void |
||
69 | { |
||
70 | $replayCount = $this->getStoredEventClass()::startingFrom($startingFrom)->count(); |
||
0 ignored issues
–
show
|
|||
71 | |||
72 | if ($replayCount === 0) { |
||
73 | $this->warn('There are no events to replay'); |
||
74 | |||
75 | return; |
||
76 | } |
||
77 | |||
78 | $this->comment("Replaying {$replayCount} events..."); |
||
79 | |||
80 | $bar = $this->output->createProgressBar($this->getStoredEventClass()::count()); |
||
0 ignored issues
–
show
|
|||
81 | $onEventReplayed = function () use ($bar) { |
||
82 | $bar->advance(); |
||
83 | }; |
||
84 | |||
85 | $this->projectionist->replay($projectors, $startingFrom, $onEventReplayed); |
||
86 | |||
87 | $bar->finish(); |
||
88 | |||
89 | $this->emptyLine(2); |
||
90 | $this->comment('All done!'); |
||
91 | } |
||
92 | |||
93 | private function emptyLine(int $amount = 1): void |
||
94 | { |
||
95 | foreach (range(1, $amount) as $i) { |
||
96 | $this->line(''); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | private function getStoredEventClass(): string |
||
101 | { |
||
102 | return config('event-projector.stored_event_model'); |
||
103 | } |
||
104 | } |
||
105 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.