Completed
Push — master ( a0d1c0...57787d )
by Freek
01:36
created

EventProjectionist::addProjector()   A

Complexity

Conditions 1
Paths 1

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