1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Spatie\EventProjector\Exceptions\InvalidEventHandler; |
7
|
|
|
|
8
|
|
|
class EventProjectionist |
9
|
|
|
{ |
10
|
|
|
/** @var \Illuminate\Support\Collection */ |
11
|
|
|
public $projectors; |
12
|
|
|
|
13
|
|
|
/** @var \Illuminate\Support\Collection */ |
14
|
|
|
public $reactors; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->projectors = collect(); |
19
|
|
|
|
20
|
|
|
$this->reactors = collect(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function addProjector($projector): self |
24
|
|
|
{ |
25
|
|
|
$this->guardAgainstInvalidEventHandler($projector); |
26
|
|
|
|
27
|
|
|
$this->projectors->push($projector); |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function registerProjectors(array $projectors): self |
33
|
|
|
{ |
34
|
|
|
collect($projectors)->each(function ($projector) { |
35
|
|
|
$this->addProjector($projector); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function addReactor($reactor): self |
42
|
|
|
{ |
43
|
|
|
$this->guardAgainstInvalidEventHandler($reactor); |
44
|
|
|
|
45
|
|
|
$this->reactors->push($reactor); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function registerReactors(array $reactors): self |
51
|
|
|
{ |
52
|
|
|
collect($reactors)->each(function ($reactor) { |
53
|
|
|
$this->addReactor($reactor); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function callEventHandlers(Collection $eventHandlers, ShouldBeStored $event): self |
60
|
|
|
{ |
61
|
|
|
$eventHandlers |
62
|
|
|
->map(function ($eventHandler) { |
63
|
|
|
|
64
|
|
|
if (is_string($eventHandler)) { |
65
|
|
|
$eventHandler = app($eventHandler); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $eventHandler; |
69
|
|
|
}) |
70
|
|
|
->each(function (object $eventHandler) use ($event) { |
71
|
|
|
$this->callEventHandler($eventHandler, $event); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function callEventHandler(object $eventHandler, ShouldBeStored $event) |
78
|
|
|
{ |
79
|
|
|
if (! isset($eventHandler->handlesEvents)) { |
80
|
|
|
throw InvalidEventHandler::cannotHandleEvents($eventHandler); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (! $method = $eventHandler->handlesEvents[get_class($event)] ?? false) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (! method_exists($eventHandler, $method)) { |
88
|
|
|
throw InvalidEventHandler::eventHandlingMethodDoesNotExist($eventHandler, $event, $method); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
app()->call([$eventHandler, $method], compact('event')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function guardAgainstInvalidEventHandler($projector): void |
95
|
|
|
{ |
96
|
|
|
if (! is_string($projector)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!class_exists($projector)) { |
101
|
|
|
throw InvalidEventHandler::doesNotExist($projector); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|