Completed
Push — master ( 1af691...12d886 )
by Julián
01:19
created

ContainerAwareQueryHandlerLocator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getHandlers() 0 22 6
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) {
0 ignored issues
show
Bug introduced by
The class Gears\CQRS\QueryHandler does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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