Skip to content

Commit

Permalink
Tweak event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Jul 13, 2020
1 parent f4057f4 commit 0f101bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
66 changes: 36 additions & 30 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Projection/EventHandlerTrait.php
Expand Up @@ -15,18 +15,18 @@
trait EventHandlerTrait
{
/** @return static */
public function applyEvent(DomainEventInterface $event): ProjectionInterface
public function applyEvent(DomainEventInterface $event): self
{
return $this->invokeEventHandler($event);
$projection = clone $this;
return $projection->invokeEventHandler($event);
}

/** @return static */
protected function invokeEventHandler(DomainEventInterface $event): ProjectionInterface
protected function invokeEventHandler(DomainEventInterface $event): self
{
$handlerName = (new ReflectionClass($event))->getShortName();
$handlerMethod = 'when'.ucfirst($handlerName);
$projection = clone $this;
$handler = [$projection, $handlerMethod];
$handler = [$this, $handlerMethod];
if (!is_callable($handler)) {
throw new RuntimeException(
sprintf("Handler '%s' is not callable on '%s'.", $handlerMethod, static::class)
Expand Down

0 comments on commit 0f101bd

Please sign in to comment.