1 | <?php |
||
17 | class Projectionist |
||
18 | { |
||
19 | /** @var array */ |
||
20 | protected $config; |
||
21 | |||
22 | /** @var \Spatie\EventProjector\EventHandlers\EventHandlerCollection */ |
||
23 | protected $projectors; |
||
24 | |||
25 | /** @var \Spatie\EventProjector\EventHandlers\EventHandlerCollection */ |
||
26 | protected $reactors; |
||
27 | |||
28 | /** @var bool */ |
||
29 | protected $isProjecting = false; |
||
30 | |||
31 | /** @var bool */ |
||
32 | protected $isReplaying = false; |
||
33 | |||
34 | public function __construct(array $config = []) |
||
35 | { |
||
36 | $this->projectors = new EventHandlerCollection(); |
||
37 | |||
38 | $this->reactors = new EventHandlerCollection(); |
||
39 | |||
40 | $this->config = $config; |
||
41 | } |
||
42 | |||
43 | public function addProjector($projector): Projectionist |
||
44 | { |
||
45 | if (is_string($projector)) { |
||
46 | $projector = app($projector); |
||
47 | } |
||
48 | |||
49 | if (! $projector instanceof Projector) { |
||
50 | throw InvalidEventHandler::notAProjector($projector); |
||
51 | } |
||
52 | |||
53 | $this->projectors->add($projector); |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | public function addProjectors(array $projectors): Projectionist |
||
59 | { |
||
60 | foreach ($projectors as $projector) { |
||
61 | $this->addProjector($projector); |
||
62 | } |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | public function getProjectors(): Collection |
||
68 | { |
||
69 | return $this->projectors->all(); |
||
70 | } |
||
71 | |||
72 | public function getProjector(string $name): ?Projector |
||
73 | { |
||
74 | return $this->projectors->all()->first(function (Projector $projector) use ($name) { |
||
75 | return $projector->getName() === $name; |
||
76 | }); |
||
77 | } |
||
78 | |||
79 | public function addReactor($reactor): Projectionist |
||
80 | { |
||
81 | $this->reactors->add($reactor); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | public function addReactors(array $reactors): Projectionist |
||
87 | { |
||
88 | foreach ($reactors as $reactor) { |
||
89 | $this->addReactor($reactor); |
||
90 | } |
||
91 | |||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | public function getReactors(): Collection |
||
96 | { |
||
97 | return $this->reactors->all(); |
||
98 | } |
||
99 | |||
100 | public function storeEvent(ShouldBeStored $event) |
||
101 | { |
||
102 | $storedEvent = $this->getStoredEventClass()::createForEvent($event); |
||
|
|||
103 | |||
104 | $this->handleImmediately($storedEvent); |
||
105 | |||
106 | if (method_exists($event, 'tags')) { |
||
107 | $tags = $event->tags(); |
||
108 | } |
||
109 | |||
110 | $storedEventJob = $this->getStoredEventJob()::createForEvent($storedEvent, $tags ?? []); |
||
111 | |||
112 | dispatch($storedEventJob->onQueue($this->config['queue'])); |
||
113 | } |
||
114 | |||
115 | public function handle(StoredEvent $storedEvent) |
||
127 | |||
128 | public function handleImmediately(StoredEvent $storedEvent) |
||
138 | |||
139 | public function isProjecting(): bool |
||
140 | { |
||
141 | return $this->isProjecting; |
||
142 | } |
||
143 | |||
144 | protected function applyStoredEventToProjectors(StoredEvent $storedEvent, Collection $projectors) |
||
145 | { |
||
146 | $this->isProjecting = true; |
||
147 | |||
148 | foreach ($projectors as $projector) { |
||
170 | |||
171 | protected function applyStoredEventToReactors(StoredEvent $storedEvent, Collection $reactors) |
||
177 | |||
178 | protected function callEventHandler(EventHandler $eventHandler, StoredEvent $storedEvent): bool |
||
196 | |||
197 | public function isReplaying(): bool |
||
201 | |||
202 | public function replay(Collection $projectors, int $afterStoredEventId = 0, callable $onEventReplayed = null) |
||
233 | |||
234 | protected function getStoredEventClass(): string |
||
238 | |||
239 | protected function getStoredEventJob(): string |
||
243 | } |
||
244 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.