Completed
Push — master ( 851b11...9f2738 )
by Freek
01:44
created

EventProjectionist::replayEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Collection;
6
use Spatie\EventProjector\Events\FinishedEventReplay;
7
use Spatie\EventProjector\Events\StartingEventReplay;
8
use Spatie\EventProjector\Exceptions\InvalidEventHandler;
9
10
class EventProjectionist
11
{
12
    /** @var \Illuminate\Support\Collection */
13
    public $projectors;
14
15
    /** @var \Illuminate\Support\Collection */
16
    public $reactors;
17
18
    /** @var boolean */
19
    protected $isReplayingEvents = false;
20
21
    public function __construct()
22
    {
23
        $this->projectors = collect();
24
25
        $this->reactors = collect();
26
    }
27
28
    public function isReplayingEvents(): bool
29
    {
30
        return $this->isReplayingEvents;
31
    }
32
33
    public function addProjector($projector): self
34
    {
35
        $this->guardAgainstInvalidEventHandler($projector);
36
37
        $this->projectors->push($projector);
38
39
        return $this;
40
    }
41
42
    public function registerProjectors(array $projectors): self
43
    {
44
        collect($projectors)->each(function ($projector) {
45
            $this->addProjector($projector);
46
        });
47
48
        return $this;
49
    }
50
51
    public function addReactor($reactor): self
52
    {
53
        $this->guardAgainstInvalidEventHandler($reactor);
54
55
        $this->reactors->push($reactor);
56
57
        return $this;
58
    }
59
60
    public function registerReactors(array $reactors): self
61
    {
62
        collect($reactors)->each(function ($reactor) {
63
            $this->addReactor($reactor);
64
        });
65
66
        return $this;
67
    }
68
69
    public function callEventHandlers(Collection $eventHandlers, ShouldBeStored $event): self
70
    {
71
        $eventHandlers
72
            ->map(function ($eventHandler) {
73
                if (is_string($eventHandler)) {
74
                    $eventHandler = app($eventHandler);
75
                }
76
77
                return $eventHandler;
78
            })
79
            ->each(function (object $eventHandler) use ($event) {
80
                $this->callEventHandler($eventHandler, $event);
81
            });
82
83
        return $this;
84
    }
85
86
    protected function callEventHandler(object $eventHandler, ShouldBeStored $event)
87
    {
88
        if (! isset($eventHandler->handlesEvents)) {
89
            throw InvalidEventHandler::cannotHandleEvents($eventHandler);
90
        }
91
92
        if (! $method = $eventHandler->handlesEvents[get_class($event)] ?? false) {
93
            return;
94
        }
95
96
        if (! method_exists($eventHandler, $method)) {
97
            throw InvalidEventHandler::eventHandlingMethodDoesNotExist($eventHandler, $event, $method);
98
        }
99
100
        app()->call([$eventHandler, $method], compact('event'));
101
    }
102
103
    public function replayEvents(Collection $projectors, callable $onEventReplayed)
104
    {
105
        $this->isReplayingEvents = true;
106
107
        event(new StartingEventReplay());
108
109
        StoredEvent::chunk(1000, function (Collection $storedEvents) use ($projectors, $onEventReplayed) {
110
            $storedEvents->each(function (StoredEvent $storedEvent) use ($projectors, $onEventReplayed) {
111
                $this->callEventHandlers($projectors, $storedEvent->event);
112
113
                $onEventReplayed($storedEvent);
114
            });
115
        });
116
117
        $this->isReplayingEvents = false;
118
119
        event(new FinishedEventReplay());
120
    }
121
122
    protected function guardAgainstInvalidEventHandler($projector)
123
    {
124
        if (! is_string($projector)) {
125
            return;
126
        }
127
128
        if (! class_exists($projector)) {
129
            throw InvalidEventHandler::doesNotExist($projector);
130
        }
131
    }
132
}
133