-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCommandBus.php
More file actions
45 lines (38 loc) · 1.38 KB
/
CommandBus.php
File metadata and controls
45 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace inklabs\kommerce\Lib\Command;
use inklabs\kommerce\Event\ReleaseEventsInterface;
use inklabs\kommerce\Lib\Authorization\AuthorizationContextInterface;
use inklabs\kommerce\Lib\Event\EventDispatcherInterface;
use inklabs\kommerce\Lib\HandlerInterface;
use inklabs\kommerce\Lib\MapperInterface;
class CommandBus implements CommandBusInterface
{
/** @var AuthorizationContextInterface */
private $authorizationContext;
/** @var MapperInterface */
private $mapper;
/** @var EventDispatcherInterface */
private $eventDispatcher;
public function __construct(
AuthorizationContextInterface $authorizationContext,
MapperInterface $mapper,
EventDispatcherInterface $eventDispatcher
) {
$this->authorizationContext = $authorizationContext;
$this->mapper = $mapper;
$this->eventDispatcher = $eventDispatcher;
}
public function execute(CommandInterface $command): void
{
$handler = $this->mapper->getCommandHandler($command);
$handler->verifyAuthorization($this->authorizationContext);
$handler->handle();
$this->dispatchEvents($handler);
}
private function dispatchEvents(HandlerInterface $handler): void
{
if ($handler instanceof ReleaseEventsInterface) {
$this->eventDispatcher->dispatchEvents($handler->releaseEvents());
}
}
}