1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Core\Cqrs\Command; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Bus\Bus; |
14
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
15
|
|
|
use Cubiche\Core\Bus\MessageInterface; |
16
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Locator\InMemoryLocator; |
17
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerClass\HandlerClassResolver; |
18
|
|
|
use Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName\MethodWithShortObjectNameResolver; |
19
|
|
|
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware; |
20
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\CommandHandlerMiddleware; |
21
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfCommand\ChainResolver as NameOfCommandChainResolver; |
22
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfCommand\FromClassNameResolver; |
23
|
|
|
use Cubiche\Core\Cqrs\Middlewares\Handler\Resolver\NameOfCommand\FromCommandNamedResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CommandBus class. |
27
|
|
|
* |
28
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class CommandBus extends Bus |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var CommandHandlerMiddleware |
34
|
|
|
*/ |
35
|
|
|
protected $commandHandlerMiddleware; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return CommandBus |
39
|
|
|
*/ |
40
|
|
|
public static function create() |
41
|
|
|
{ |
42
|
|
|
return new static([ |
43
|
|
|
250 => new LockingMiddleware(), |
44
|
|
|
100 => new CommandHandlerMiddleware(new HandlerClassResolver( |
45
|
|
|
new NameOfCommandChainResolver([ |
46
|
|
|
new FromCommandNamedResolver(), |
47
|
|
|
new FromClassNameResolver(), |
48
|
|
|
]), |
49
|
|
|
new MethodWithShortObjectNameResolver('Command'), |
50
|
|
|
new InMemoryLocator() |
51
|
|
|
)), |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function dispatch(MessageInterface $command) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
if (!$command instanceof CommandInterface) { |
61
|
|
|
throw new \InvalidArgumentException( |
62
|
|
|
sprintf( |
63
|
|
|
'The object must be an instance of %s. Instance of %s given', |
64
|
|
|
CommandInterface::class, |
65
|
|
|
get_class($command) |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->ensureCommandHandlerMiddleware(); |
71
|
|
|
|
72
|
|
|
parent::dispatch($command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ensure that exists an command handler middleware. |
77
|
|
|
* |
78
|
|
|
* @throws NotFoundException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function ensureCommandHandlerMiddleware() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if ($this->commandHandlerMiddleware !== null) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($this->middlewares as $priority => $collection) { |
87
|
|
|
foreach ($collection as $middleware) { |
88
|
|
|
if ($middleware instanceof CommandHandlerMiddleware) { |
89
|
|
|
$this->commandHandlerMiddleware = $middleware; |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
throw NotFoundException::middlewareOfType(CommandHandlerMiddleware::class); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return CommandHandlerMiddleware |
101
|
|
|
*/ |
102
|
|
|
public function handlerMiddleware() |
103
|
|
|
{ |
104
|
|
|
$this->ensureCommandHandlerMiddleware(); |
105
|
|
|
|
106
|
|
|
return $this->commandHandlerMiddleware; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $commandName |
111
|
|
|
* @param mixed $commandHandler |
112
|
|
|
*/ |
113
|
|
|
public function addHandler($commandName, $commandHandler) |
114
|
|
|
{ |
115
|
|
|
$this->ensureCommandHandlerMiddleware(); |
116
|
|
|
|
117
|
|
|
$this->commandHandlerMiddleware->resolver()->addHandler($commandName, $commandHandler); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $commandName |
122
|
|
|
* |
123
|
|
|
* @return object |
124
|
|
|
*/ |
125
|
|
|
public function getHandlerFor($commandName) |
126
|
|
|
{ |
127
|
|
|
$this->ensureCommandHandlerMiddleware(); |
128
|
|
|
|
129
|
|
|
return $this->commandHandlerMiddleware->resolver()->getHandlerFor($commandName); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.