Completed
Push — master ( 64d9c2...789991 )
by Freek
02:03
created

ReplayEventsCommand::commandShouldRun()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $onlyCallProjectors defined by $this->option('projector') on line 56 can also be of type string; however, Spatie\EventProjector\Co...NonExistingProjectors() 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...
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