Skip to content

Commit

Permalink
Merge pull request #111 from proophsoftware/feature/stop_dispatch_wit…
Browse files Browse the repository at this point in the history
…h_preprocessor

Allow a preprocessor to stop dispatch
  • Loading branch information
codeliner authored Nov 27, 2018
2 parents b3e67b2 + 9882297 commit 0eb2881
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/PrototypingFlavour/Messaging/CommandWithCustomHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of the proophsoftware/event-machine.
* (c) 2017-2018 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ProophExample\PrototypingFlavour\Messaging;

use Prooph\EventMachine\EventMachine;
use Prooph\EventMachine\EventMachineDescription;
use Prooph\EventMachine\JsonSchema\JsonSchema;

final class CommandWithCustomHandler implements EventMachineDescription
{
public const CMD_DO_NOTHING = 'DoNothing';
public const NO_OP_HANDLER = 'NoOpHandler';

public static function describe(EventMachine $eventMachine): void
{
$eventMachine->registerCommand(self::CMD_DO_NOTHING, JsonSchema::object(['msg' => JsonSchema::string()]));
$eventMachine->preProcess(self::CMD_DO_NOTHING, self::NO_OP_HANDLER);
}
}
6 changes: 6 additions & 0 deletions src/EventMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ final class EventMachine implements MessageDispatcher, AggregateStateStore
const SERVICE_ID_JSON_SCHEMA_ASSERTION = 'EventMachine.JsonSchemaAssertion';
const SERVICE_ID_FLAVOUR = 'EventMachine.Flavour';

const CMD_METADATA_STOP_DISPATCH = 'EVENT-MACHINE-STOP-DISPATCH';

/**
* Map of command names and corresponding json schema of payload
*
Expand Down Expand Up @@ -539,6 +541,10 @@ public function dispatch($messageOrName, array $payload = []): ?Promise
}

$messageOrName = $this->flavour()->callCommandPreProcessor($preProcessorOrStr, $messageOrName);

if ($messageOrName->metadata()[self::CMD_METADATA_STOP_DISPATCH] ?? false) {
return null;
}
}

$bus = $this->container->get(self::SERVICE_ID_COMMAND_BUS);
Expand Down
48 changes: 48 additions & 0 deletions tests/EventMachinePrototypingFlavourTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace Prooph\EventMachineTest;

use Prooph\Common\Messaging\Message as ProophMessage;
use Prooph\EventMachine\Commanding\CommandPreProcessor;
use Prooph\EventMachine\Container\EventMachineContainer;
use Prooph\EventMachine\EventMachine;
use Prooph\EventMachine\Messaging\Message;
use Prooph\EventMachine\Messaging\MessageDispatcher;
use Prooph\EventMachine\Persistence\DocumentStore;
use Prooph\EventMachine\Runtime\Flavour;
use Prooph\EventMachine\Runtime\PrototypingFlavour;
use ProophExample\PrototypingFlavour\Aggregate\UserDescription;
use ProophExample\PrototypingFlavour\Aggregate\UserState;
use ProophExample\PrototypingFlavour\Messaging\CommandWithCustomHandler;
use ProophExample\PrototypingFlavour\Messaging\MessageDescription;
use ProophExample\PrototypingFlavour\ProcessManager\SendWelcomeEmail;
use ProophExample\PrototypingFlavour\Projector\RegisteredUsersProjector;
Expand Down Expand Up @@ -83,4 +88,47 @@ public function it_throws_exception_if_config_should_be_cached_but_contains_clos

$eventMachine->compileCacheableConfig();
}

/**
* @test
*/
public function it_stops_dispatch_if_preprocessor_sets_metadata_flag()
{
$eventMachine = new EventMachine();

$eventMachine->load(CommandWithCustomHandler::class);

$noOpHandler = new class() implements CommandPreProcessor {
private $msg;

/**
* {@inheritdoc}
*/
public function preProcess(ProophMessage $message): ProophMessage
{
if ($message instanceof Message) {
$this->msg = $message->get('msg');
}

return $message->withAddedMetadata(EventMachine::CMD_METADATA_STOP_DISPATCH, true);
}

public function msg(): ?string
{
return $this->msg;
}
};

$eventMachine->initialize(new EventMachineContainer($eventMachine));

$eventMachine->bootstrapInTestMode([], [
CommandWithCustomHandler::NO_OP_HANDLER => $noOpHandler,
]);

$eventMachine->dispatch(CommandWithCustomHandler::CMD_DO_NOTHING, [
'msg' => 'test',
]);

$this->assertEquals('test', $noOpHandler->msg());
}
}

0 comments on commit 0eb2881

Please sign in to comment.