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\QueryHandler; |
17
|
|
|
use Gears\CQRS\Symfony\Messenger\Exception\InvalidQueryHandlerException; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
use Symfony\Component\Messenger\Envelope; |
20
|
|
|
|
21
|
|
|
class ContainerAwareQueryHandlerLocator extends QueryHandlerLocator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* PSR container. |
25
|
|
|
* |
26
|
|
|
* @var ContainerInterface |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ContainerAwareCommandHandlerLocator constructor. |
32
|
|
|
* |
33
|
|
|
* @param ContainerInterface $container |
34
|
|
|
* @param mixed[] $handlers |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ContainerInterface $container, array $handlers) |
37
|
|
|
{ |
38
|
|
|
$this->container = $container; |
39
|
|
|
|
40
|
|
|
parent::__construct($handlers); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
* |
46
|
|
|
* @throws InvalidQueryHandlerException |
47
|
|
|
*/ |
48
|
|
|
public function getHandlers(Envelope $envelope): iterable |
49
|
|
|
{ |
50
|
|
|
$seen = []; |
51
|
|
|
|
52
|
|
|
foreach ($this->getQueryMap($envelope) as $type) { |
53
|
|
|
foreach ($this->handlersMap[$type] ?? [] as $alias => $handler) { |
54
|
|
|
$handler = $this->container->get($handler); |
55
|
|
|
|
56
|
|
|
if (!$handler instanceof QueryHandler) { |
|
|
|
|
57
|
|
|
throw new InvalidQueryHandlerException(\sprintf( |
58
|
|
|
'Query handler must implement %s interface, %s given', |
59
|
|
|
QueryHandler::class, |
60
|
|
|
\is_object($handler) ? \get_class($handler) : \gettype($handler) |
61
|
|
|
)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!\in_array($handler, $seen, true)) { |
65
|
|
|
yield $alias => $seen[] = $handler; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.