Skip to content

Commit 0eb2881

Browse files
author
Alexander Miertsch
authored
Merge pull request #111 from proophsoftware/feature/stop_dispatch_with_preprocessor
Allow a preprocessor to stop dispatch
2 parents b3e67b2 + 9882297 commit 0eb2881

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* This file is part of the proophsoftware/event-machine.
4+
* (c) 2017-2018 prooph software GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace ProophExample\PrototypingFlavour\Messaging;
13+
14+
use Prooph\EventMachine\EventMachine;
15+
use Prooph\EventMachine\EventMachineDescription;
16+
use Prooph\EventMachine\JsonSchema\JsonSchema;
17+
18+
final class CommandWithCustomHandler implements EventMachineDescription
19+
{
20+
public const CMD_DO_NOTHING = 'DoNothing';
21+
public const NO_OP_HANDLER = 'NoOpHandler';
22+
23+
public static function describe(EventMachine $eventMachine): void
24+
{
25+
$eventMachine->registerCommand(self::CMD_DO_NOTHING, JsonSchema::object(['msg' => JsonSchema::string()]));
26+
$eventMachine->preProcess(self::CMD_DO_NOTHING, self::NO_OP_HANDLER);
27+
}
28+
}

src/EventMachine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ final class EventMachine implements MessageDispatcher, AggregateStateStore
8787
const SERVICE_ID_JSON_SCHEMA_ASSERTION = 'EventMachine.JsonSchemaAssertion';
8888
const SERVICE_ID_FLAVOUR = 'EventMachine.Flavour';
8989

90+
const CMD_METADATA_STOP_DISPATCH = 'EVENT-MACHINE-STOP-DISPATCH';
91+
9092
/**
9193
* Map of command names and corresponding json schema of payload
9294
*
@@ -539,6 +541,10 @@ public function dispatch($messageOrName, array $payload = []): ?Promise
539541
}
540542

541543
$messageOrName = $this->flavour()->callCommandPreProcessor($preProcessorOrStr, $messageOrName);
544+
545+
if ($messageOrName->metadata()[self::CMD_METADATA_STOP_DISPATCH] ?? false) {
546+
return null;
547+
}
542548
}
543549

544550
$bus = $this->container->get(self::SERVICE_ID_COMMAND_BUS);

tests/EventMachinePrototypingFlavourTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111

1212
namespace Prooph\EventMachineTest;
1313

14+
use Prooph\Common\Messaging\Message as ProophMessage;
15+
use Prooph\EventMachine\Commanding\CommandPreProcessor;
16+
use Prooph\EventMachine\Container\EventMachineContainer;
1417
use Prooph\EventMachine\EventMachine;
18+
use Prooph\EventMachine\Messaging\Message;
1519
use Prooph\EventMachine\Messaging\MessageDispatcher;
1620
use Prooph\EventMachine\Persistence\DocumentStore;
1721
use Prooph\EventMachine\Runtime\Flavour;
1822
use Prooph\EventMachine\Runtime\PrototypingFlavour;
1923
use ProophExample\PrototypingFlavour\Aggregate\UserDescription;
2024
use ProophExample\PrototypingFlavour\Aggregate\UserState;
25+
use ProophExample\PrototypingFlavour\Messaging\CommandWithCustomHandler;
2126
use ProophExample\PrototypingFlavour\Messaging\MessageDescription;
2227
use ProophExample\PrototypingFlavour\ProcessManager\SendWelcomeEmail;
2328
use ProophExample\PrototypingFlavour\Projector\RegisteredUsersProjector;
@@ -83,4 +88,47 @@ public function it_throws_exception_if_config_should_be_cached_but_contains_clos
8388

8489
$eventMachine->compileCacheableConfig();
8590
}
91+
92+
/**
93+
* @test
94+
*/
95+
public function it_stops_dispatch_if_preprocessor_sets_metadata_flag()
96+
{
97+
$eventMachine = new EventMachine();
98+
99+
$eventMachine->load(CommandWithCustomHandler::class);
100+
101+
$noOpHandler = new class() implements CommandPreProcessor {
102+
private $msg;
103+
104+
/**
105+
* {@inheritdoc}
106+
*/
107+
public function preProcess(ProophMessage $message): ProophMessage
108+
{
109+
if ($message instanceof Message) {
110+
$this->msg = $message->get('msg');
111+
}
112+
113+
return $message->withAddedMetadata(EventMachine::CMD_METADATA_STOP_DISPATCH, true);
114+
}
115+
116+
public function msg(): ?string
117+
{
118+
return $this->msg;
119+
}
120+
};
121+
122+
$eventMachine->initialize(new EventMachineContainer($eventMachine));
123+
124+
$eventMachine->bootstrapInTestMode([], [
125+
CommandWithCustomHandler::NO_OP_HANDLER => $noOpHandler,
126+
]);
127+
128+
$eventMachine->dispatch(CommandWithCustomHandler::CMD_DO_NOTHING, [
129+
'msg' => 'test',
130+
]);
131+
132+
$this->assertEquals('test', $noOpHandler->msg());
133+
}
86134
}

0 commit comments

Comments
 (0)