Completed
Pull Request — master (#17)
by Sebastian
01:48
created

EventProjectionist::getProjectors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Collection;
6
use Spatie\EventProjector\Models\StoredEvent;
7
use Spatie\EventProjector\Projectors\Projector;
8
use Spatie\EventProjector\Events\FinishedEventReplay;
9
use Spatie\EventProjector\Events\StartingEventReplay;
10
use Spatie\EventProjector\Exceptions\InvalidEventHandler;
11
use Spatie\EventProjector\Events\ProjectorDidNotHandlePriorEvents;
12
13
class EventProjectionist
14
{
15
    /** @var \Illuminate\Support\Collection */
16
    protected $projectors;
17
18
    /** @var \Illuminate\Support\Collection */
19
    protected $reactors;
20
21
    /** @var bool */
22
    protected $isReplayingEvents = false;
23
24
    /** @var int */
25
    protected $replayChunkSize;
26
27
    public function __construct(array $config)
28
    {
29
        $this->projectors = collect();
30
31
        $this->reactors = collect();
32
33
        $this->replayChunkSize = $config['replay_chunk_size'] ?? 1000;
34
    }
35
36
    public function isReplayingEvents(): bool
37
    {
38
        return $this->isReplayingEvents;
39
    }
40
41
    public function addProjector($projector): self
42
    {
43
        $this->guardAgainstInvalidEventHandler($projector);
44
45
        $this->projectors->push($projector);
46
47
        return $this;
48
    }
49
50
    public function addProjectors(array $projectors): self
51
    {
52
        collect($projectors)->each(function ($projector) {
53
            $this->addProjector($projector);
54
        });
55
56
        return $this;
57
    }
58
59
    public function getProjectors(): Collection
60
    {
61
        return $this->projectors;
62
    }
63
64
    public function addReactor($reactor): self
65
    {
66
        $this->guardAgainstInvalidEventHandler($reactor);
67
68
        $this->reactors->push($reactor);
69
70
        return $this;
71
    }
72
73
    public function addReactors(array $reactors): self
74
    {
75
        collect($reactors)->each(function ($reactor) {
76
            $this->addReactor($reactor);
77
        });
78
79
        return $this;
80
    }
81
82
    public function handle(StoredEvent $storedEvent)
83
    {
84
        $this
85
            ->callEventHandlers($this->projectors, $storedEvent)
86
            ->callEventHandlers($this->reactors, $storedEvent);
87
    }
88
89
    protected function callEventHandlers(Collection $eventHandlers, StoredEvent $storedEvent): self
90
    {
91
        $eventHandlers
92
            ->pipe(function (Collection $eventHandler) {
93
                return $this->instantiate($eventHandler);
94
            })
95
            ->filter(function (object $eventHandler) use ($storedEvent) {
96
                if ($eventHandler instanceof Projector) {
97
                    if (! $eventHandler->hasReceivedAllPriorEvents($storedEvent)) {
98
                        event(new ProjectorDidNotHandlePriorEvents($eventHandler, $storedEvent));
99
100
                        return false;
101
                    }
102
                }
103
104
                return true;
105
            })
106
            ->each(function (object $eventHandler) use ($storedEvent) {
107
                $this->callEventHandler($eventHandler, $storedEvent);
108
109
                if ($eventHandler instanceof Projector) {
110
                    $eventHandler->rememberReceivedEvent($storedEvent);
111
                }
112
            });
113
114
        return $this;
115
    }
116
117
    protected function callEventHandler(object $eventHandler, StoredEvent $storedEvent)
118
    {
119
        if (! isset($eventHandler->handlesEvents)) {
120
            throw InvalidEventHandler::cannotHandleEvents($eventHandler);
121
        }
122
123
        $event = $storedEvent->event;
124
125
        if (! $method = $eventHandler->handlesEvents[get_class($event)] ?? false) {
126
            return;
127
        }
128
129
        if (! method_exists($eventHandler, $method)) {
130
            throw InvalidEventHandler::eventHandlingMethodDoesNotExist($eventHandler, $event, $method);
131
        }
132
133
        app()->call([$eventHandler, $method], compact('event', 'storedEvent'));
134
    }
135
136
    public function replayEvents(Collection $projectors, callable $onEventReplayed)
137
    {
138
        $this->isReplayingEvents = true;
139
140
        event(new StartingEventReplay());
141
142
        $projectors = $this
143
            ->instantiate($projectors)
144
            ->each->resetStatus();
145
146
        $this->callMethod($projectors, 'onStartingEventReplay');
147
148
        StoredEvent::chunk($this->replayChunkSize, function (Collection $storedEvents) use ($projectors, $onEventReplayed) {
149
            $storedEvents->each(function (StoredEvent $storedEvent) use ($projectors, $onEventReplayed) {
150
                $this->callEventHandlers($projectors, $storedEvent);
151
152
                $onEventReplayed($storedEvent);
153
            });
154
        });
155
156
        $this->isReplayingEvents = false;
157
158
        event(new FinishedEventReplay());
159
160
        $this->callMethod($projectors, 'onFinishedEventReplay');
161
    }
162
163
    protected function guardAgainstInvalidEventHandler($eventHandler)
164
    {
165
        if (! is_string($eventHandler)) {
166
            return;
167
        }
168
169
        if (! class_exists($eventHandler)) {
170
            throw InvalidEventHandler::doesNotExist($eventHandler);
171
        }
172
    }
173
174
    protected function instantiate(Collection $eventHandlers)
175
    {
176
        return $eventHandlers->map(function ($eventHandler) {
177
            if (is_string($eventHandler)) {
178
                $eventHandler = app($eventHandler);
179
            }
180
181
            return $eventHandler;
182
        });
183
    }
184
185
    protected function callMethod(Collection $eventHandlers, string $method): self
186
    {
187
        $eventHandlers
188
            ->filter(function (object $eventHandler) use ($method) {
189
                return method_exists($eventHandler, $method);
190
            })
191
            ->each(function (object $eventHandler) use ($method) {
192
                return app()->call([$eventHandler, $method]);
193
            });
194
195
        return $this;
196
    }
197
}
198