Skip to content

Instantly share code, notes, and snippets.

@codeliner
Created November 27, 2018 20:43
Show Gist options
  • Save codeliner/144b90ab4dee3fd3016f0df1e4d340c9 to your computer and use it in GitHub Desktop.
Save codeliner/144b90ab4dee3fd3016f0df1e4d340c9 to your computer and use it in GitHub Desktop.
Event Machine Custom Command Handler Flavour
<?php
declare(strict_types=1);
namespace ProophExample\CommandHandler;
use Prooph\EventMachine\Messaging\Message;
interface CommandHandler
{
public function handle(Message $message): void;
}
<?php
declare(strict_types=1);
namespace ProophExample\CommandHandler;
use Prooph\EventMachine\EventMachine;
use Prooph\EventMachine\Messaging\Message;
use Prooph\EventMachine\Runtime\Flavour;
use Prooph\EventMachine\Runtime\PrototypingFlavour;
use React\Promise\Deferred;
/**
* Class CommandHandlerFlavour
*
* This Flavour hooks into command preprocessor calls to use a configured preprocessor as a custom command handler
* if it implements the interface ProophExample\CommandHandler\CommandHandler
*
* In that case command is passed to the handler and dispatch is stopped afterwards.
*
* All other methods are just proxies to the main Flavour.
*
* @package ProophExample\CommandHandler
*/
final class CommandHandlerFlavour implements Flavour
{
/**
* You could also use another main Flavour!
*
* @var PrototypingFlavour
*/
private $mainFlavour;
public function __construct(PrototypingFlavour $mainFlavour)
{
$this->mainFlavour = $mainFlavour;
}
/**
* @inheritdoc
*/
public function callCommandPreProcessor($preProcessor, Message $command): Message
{
if(!$preProcessor instanceof CommandHandler) {
return $this->mainFlavour->callCommandPreProcessor($preProcessor, $command);
}
$preProcessor->handle($command);
//We can tell Event Machine that no further processing is needed for that command
return $command->withAddedMetadata(EventMachine::CMD_METADATA_STOP_DISPATCH, true);
}
/**
* @inheritdoc
*/
public function getAggregateIdFromCommand(string $aggregateIdPayloadKey, Message $command): string
{
return $this->mainFlavour->getAggregateIdFromCommand($aggregateIdPayloadKey, $command);
}
/**
* @inheritdoc
*/
public function callContextProvider($contextProvider, Message $command)
{
return $this->mainFlavour->callContextProvider($contextProvider, $command);
}
/**
* @inheritdoc
*/
public function callAggregateFactory(string $aggregateType, callable $aggregateFunction, Message $command, $context = null): \Generator
{
return $this->mainFlavour->callAggregateFactory($aggregateType, $aggregateFunction, $command, $context);
}
/**
* @inheritdoc
*/
public function callSubsequentAggregateFunction(string $aggregateType, callable $aggregateFunction, $aggregateState, Message $command, $context = null): \Generator
{
return $this->mainFlavour->callSubsequentAggregateFunction($aggregateType, $aggregateFunction, $aggregateState, $command, $context);
}
/**
* @inheritdoc
*/
public function callApplyFirstEvent(callable $applyFunction, Message $event)
{
return $this->mainFlavour->callApplyFirstEvent($applyFunction, $event);
}
/**
* @inheritdoc
*/
public function callApplySubsequentEvent(callable $applyFunction, $aggregateState, Message $event)
{
return $this->callApplySubsequentEvent($applyFunction, $aggregateState, $event);
}
/**
* @inheritdoc
*/
public function prepareNetworkTransmission(Message $message): Message
{
return $this->mainFlavour->prepareNetworkTransmission($message);
}
/**
* @inheritdoc
*/
public function convertMessageReceivedFromNetwork(Message $message, $firstAggregateEvent = false): Message
{
return $this->mainFlavour->convertMessageReceivedFromNetwork($message, $firstAggregateEvent);
}
/**
* @inheritdoc
*/
public function callProjector($projector, string $appVersion, string $projectionName, Message $event): void
{
$this->mainFlavour->callProjector($projector, $appVersion, $projectionName, $event);
}
/**
* @inheritdoc
*/
public function convertAggregateStateToArray($aggregateState): array
{
return $this->mainFlavour->convertAggregateStateToArray($aggregateState);
}
/**
* @inheritdoc
*/
public function callEventListener(callable $listener, Message $event): void
{
$this->mainFlavour->callEventListener($listener, $event);
}
/**
* @inheritdoc
*/
public function callQueryResolver(callable $resolver, Message $query, Deferred $deferred): void
{
$this->mainFlavour->callQueryResolver($resolver, $query, $deferred);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment