Skip to content

Commit

Permalink
Merge pull request #6 from zumba/CLS-38-allow-explicit-vals
Browse files Browse the repository at this point in the history
CLS-38 Allow Passing Explicit Values To Event Map
  • Loading branch information
NerdyHouse authored Aug 9, 2021
2 parents 1695d27 + b5bf05d commit 2b1c522
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Command/EventMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class EventMap
*/
protected $map;

/**
* @var array<string, array>
*/
protected $staticProperties = [];

/**
* The logger instance.
*
Expand Down Expand Up @@ -84,6 +89,19 @@ public function withLogger(LoggerInterface $logger): EventMap
return $map;
}

/**
* Attach explicit / arbitrary values to en event map
* The listening map should know very little about the event it is listening for
*
* @param array<string, array> $properties
*/
public function withStaticProperties(array $properties): EventMap
{
$eventMap = clone $this;
$eventMap->staticProperties = $properties;
return $eventMap;
}

/**
* Get a map of event names to callables that will handle the events and bus commands
*
Expand All @@ -109,7 +127,15 @@ protected function listener(CommandBus $bus, array $commands): callable
return function (object $event) use ($bus, $commands): void {
foreach ($commands as $command => $map) {
if (in_array(WithProperties::class, class_implements($command) ?: [])) {
$instance = ((string)$command)::fromArray($this->transform($event, $map));
if ($event instanceof \Zumba\Symbiosis\Framework\EventInterface) {
$commandsWithStaticProps = $this->staticProperties[$event->name()] ?? [];
$staticProperties = $commandsWithStaticProps[$command] ?? [];
foreach ($staticProperties as $key => $val) {
$staticProperties[$key] = $this->transformValue($val);
}
}
$props = $this->transform($event, $map) + ($staticProperties ?? []);
$instance = ((string)$command)::fromArray($props);
} else {
$instance = new $command();
}
Expand Down

0 comments on commit 2b1c522

Please sign in to comment.