1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* cqrs-symfony-messenger (https://github.com/phpgears/cqrs-symfony-messenger). |
5
|
|
|
* CQRS implementation with Symfony's Messenger. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/cqrs-symfony-messenger |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\CQRS\Symfony\Messenger; |
15
|
|
|
|
16
|
|
|
use Gears\CQRS\Command; |
17
|
|
|
use Gears\CQRS\CommandHandler; |
18
|
|
|
use Gears\CQRS\Exception\InvalidCommandException; |
19
|
|
|
use Gears\CQRS\Symfony\Messenger\Exception\InvalidCommandHandlerException; |
20
|
|
|
use Symfony\Component\Messenger\Envelope; |
21
|
|
|
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface; |
22
|
|
|
|
23
|
|
|
class CommandHandlerLocator implements HandlersLocatorInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Command handlers map. |
27
|
|
|
* |
28
|
|
|
* @var mixed[] |
29
|
|
|
*/ |
30
|
|
|
protected $handlersMap; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* CommandHandlerLocator constructor. |
34
|
|
|
* |
35
|
|
|
* @param mixed[] $handlers |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $handlers) |
38
|
|
|
{ |
39
|
|
|
$this->handlersMap = $handlers; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* |
45
|
|
|
* @throws InvalidCommandHandlerException |
46
|
|
|
*/ |
47
|
|
|
public function getHandlers(Envelope $envelope): iterable |
48
|
|
|
{ |
49
|
|
|
$seen = []; |
50
|
|
|
|
51
|
|
|
foreach ($this->getCommandMap($envelope) as $type) { |
52
|
|
|
foreach ($this->handlersMap[$type] ?? [] as $alias => $handler) { |
53
|
|
|
if (!$handler instanceof CommandHandler) { |
|
|
|
|
54
|
|
|
throw new InvalidCommandHandlerException(\sprintf( |
55
|
|
|
'Command handler must implement %s interface, %s given', |
56
|
|
|
CommandHandler::class, |
57
|
|
|
\is_object($handler) ? \get_class($handler) : \gettype($handler) |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!\in_array($handler, $seen, true)) { |
62
|
|
|
yield $alias => $seen[] = $handler; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get command mapping. |
70
|
|
|
* |
71
|
|
|
* @param Envelope $envelope |
72
|
|
|
* |
73
|
|
|
* @throws InvalidCommandException |
74
|
|
|
* |
75
|
|
|
* @return mixed[] |
76
|
|
|
*/ |
77
|
|
|
final protected function getCommandMap(Envelope $envelope): array |
78
|
|
|
{ |
79
|
|
|
$command = $envelope->getMessage(); |
80
|
|
|
|
81
|
|
|
if (!$command instanceof Command) { |
|
|
|
|
82
|
|
|
throw new InvalidCommandException(\sprintf( |
83
|
|
|
'Command must implement %s interface, %s given', |
84
|
|
|
Command::class, |
85
|
|
|
\is_object($command) ? \get_class($command) : \gettype($command) |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$class = \get_class($command); |
90
|
|
|
|
91
|
|
|
return [$class => $class] |
92
|
|
|
+ ['*' => '*']; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.