Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed May 21, 2018
1 parent 9f2738f commit 4e0e24b
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/EventProjectionist.php
Expand Up @@ -69,12 +69,8 @@ public function registerReactors(array $reactors): self
public function callEventHandlers(Collection $eventHandlers, ShouldBeStored $event): self
{
$eventHandlers
->map(function ($eventHandler) {
if (is_string($eventHandler)) {
$eventHandler = app($eventHandler);
}

return $eventHandler;
->pipe(function(Collection $eventHandler) {
return $this->instanciate($eventHandler);
})
->each(function (object $eventHandler) use ($event) {
$this->callEventHandler($eventHandler, $event);
Expand All @@ -85,15 +81,15 @@ public function callEventHandlers(Collection $eventHandlers, ShouldBeStored $eve

protected function callEventHandler(object $eventHandler, ShouldBeStored $event)
{
if (! isset($eventHandler->handlesEvents)) {
if (!isset($eventHandler->handlesEvents)) {
throw InvalidEventHandler::cannotHandleEvents($eventHandler);
}

if (! $method = $eventHandler->handlesEvents[get_class($event)] ?? false) {
if (!$method = $eventHandler->handlesEvents[get_class($event)] ?? false) {
return;
}

if (! method_exists($eventHandler, $method)) {
if (!method_exists($eventHandler, $method)) {
throw InvalidEventHandler::eventHandlingMethodDoesNotExist($eventHandler, $event, $method);
}

Expand All @@ -102,10 +98,14 @@ protected function callEventHandler(object $eventHandler, ShouldBeStored $event)

public function replayEvents(Collection $projectors, callable $onEventReplayed)
{
$projectors = $this->instanciate($projectors);

$this->isReplayingEvents = true;

event(new StartingEventReplay());

$this->callMethod($projectors, 'onStartingEventReplay');

StoredEvent::chunk(1000, function (Collection $storedEvents) use ($projectors, $onEventReplayed) {
$storedEvents->each(function (StoredEvent $storedEvent) use ($projectors, $onEventReplayed) {
$this->callEventHandlers($projectors, $storedEvent->event);
Expand All @@ -117,16 +117,42 @@ public function replayEvents(Collection $projectors, callable $onEventReplayed)
$this->isReplayingEvents = false;

event(new FinishedEventReplay());

$this->callMethod($projectors, 'onFinishedEventReplay');
}

protected function guardAgainstInvalidEventHandler($projector)
{
if (! is_string($projector)) {
if (!is_string($projector)) {
return;
}

if (! class_exists($projector)) {
if (!class_exists($projector)) {
throw InvalidEventHandler::doesNotExist($projector);
}
}

protected function instanciate(Collection $eventHandlers)
{
return $eventHandlers->map(function ($eventHandler) {
if (is_string($eventHandler)) {
$eventHandler = app($eventHandler);
}

return $eventHandler;
});
}

protected function callMethod(Collection $eventHandlers, string $method): self
{
$eventHandlers
->filter(function (object $eventHandler) use ($method) {
return method_exists($eventHandler, $method);
})
->each(function (object $eventHandler) use ($method) {
return app()->call([$eventHandler, $method]);
});

return $this;
}
}

0 comments on commit 4e0e24b

Please sign in to comment.