Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/EventAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Webware\CommandBus\Event;

interface EventAwareInterface
{
public function getEvent(): Event;

public function setEvent(Event $event): void;
}
24 changes: 24 additions & 0 deletions src/EventAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Webware\CommandBus\Event;

use Override;

trait EventAwareTrait
{
private ?EventInterface $event = null;

#[Override]
public function getEvent(): ?EventInterface
{
return $this->event;
}

#[Override]
public function setEvent(EventInterface $event): void
{
$this->event = $event;
}
}
10 changes: 9 additions & 1 deletion src/Middleware/PostHandleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Webware\CommandBus\Command\CommandResultInterface;
use Webware\CommandBus\CommandHandlerInterface;
use Webware\CommandBus\CommandInterface;
use Webware\CommandBus\Event\EventAwareInterface;
use Webware\CommandBus\Event\EventDispatcherAwareInterface;
use Webware\CommandBus\Event\EventDispatcherAwareTrait;
use Webware\CommandBus\Event\PostHandleEvent;
Expand All @@ -33,8 +34,15 @@ public function process(
CommandHandlerInterface $handler,
): CommandResultInterface {
// Custom processing logic for this middleware
$dispatcher = $this->getEventDispatcher();

if ($command instanceof EventAwareInterface) {
if ($event = $command->getEvent()) {
$dispatcher->dispatch($event);
}
}
if ($command instanceof CommandResultInterface) {
$this->getEventDispatcher()->dispatch(new PostHandleEvent($command));
$dispatcher->dispatch(new PostHandleEvent($command));

// Return the CommandResult
return $command;
Expand Down