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

ReplayEventsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 0 24 2
A getMutators() 0 15 2
A guardAgainstNonExistingMutators() 0 8 3
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