Completed
Push — master ( 0ba1f3...e49bb9 )
by Freek
03:02
created

guardAgainstNonExistingMutators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
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
                            {--mutator=*} : The mutator that should receive the event';
15
16
    protected $description = 'Replay stored events';
17
18
    /** @var \Spatie\EventProjector\EventProjectionist */
19
    protected $eventSorcerer;
20
21
    public function __construct(EventProjectionist $eventSorcerer)
22
    {
23
        parent::__construct();
24
25
        $this->eventSorcerer = $eventSorcerer;
26
    }
27
28
    public function handle()
29
    {
30
        $mutators = $this->getMutators();
31
32
        if ($mutators->isEmpty()) {
33
            $this->warn('No mutators found to replay events to...');
34
35
            return;
36
        }
37
38
        $this->comment('Replaying events...');
39
40
        $bar = $this->output->createProgressBar(StoredEvent::count());
41
42
        StoredEvent::chunk(1000, function (StoredEvent $storedEvent) use ($mutators, $bar) {
43
            $this->eventSorcerer->callEventHandlers($mutators, $storedEvent);
44
45
            $bar->advance();
46
        });
47
48
        $bar->finish();
49
50
        $this->comment('All done!');
51
    }
52
53
    protected function getMutators(): Collection
54
    {
55
        $onlyCallMutators = $this->option('mutator');
56
57
        $this->guardAgainstNonExistingMutators($onlyCallMutators);
0 ignored issues
show
Bug introduced by
It seems like $onlyCallMutators defined by $this->option('mutator') on line 55 can also be of type string; however, Spatie\EventProjector\Co...stNonExistingMutators() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
59
        return $this->eventSorcerer->mutators
60
            ->filter(function (string $mutator) use ($onlyCallMutators) {
61
                if (! count($onlyCallMutators)) {
62
                    return true;
63
                }
64
65
                return in_array($mutator, $onlyCallMutators);
66
            });
67
    }
68
69
    protected function guardAgainstNonExistingMutators(array $onlyCallMutators)
70
    {
71
        foreach ($onlyCallMutators as $mutator) {
72
            if (! class_exists($mutator)) {
73
                throw InvalidEventHandler::doesNotExist($mutator);
74
            }
75
        }
76
    }
77
}
78