Last active
August 29, 2015 14:26
-
-
Save nicholasruunu/d29ba4719d7ba89bc8b3 to your computer and use it in GitHub Desktop.
DomainEventProxy trait exmple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait DomainEventProxy | |
{ | |
private $eventMethodMapping = []; | |
private function callMappedMethod(DomainEvent $event, ...$arguments) | |
{ | |
$method = $this->resolveMethod($event); | |
$this->$method($event, ...$arguments); | |
} | |
private function resolveMethod(DomainEvent $event) | |
{ | |
$eventName = (new ReflectionClass($event))->getShortName(); | |
if (!array_key_exists($eventName, $this->eventMethodMapping)) { | |
throw Exception("$eventName has not been mapped."); | |
} | |
return $this->eventMethodMapping[$eventName]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment