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

QueryHandlerLocator::getHandlersMap()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 5
nop 1
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\Exception\InvalidQueryException;
17
use Gears\CQRS\Query;
18
use Gears\CQRS\QueryHandler;
19
use Gears\CQRS\Symfony\Messenger\Exception\InvalidQueryHandlerException;
20
use Symfony\Component\Messenger\Envelope;
21
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
22
23
class QueryHandlerLocator implements HandlersLocatorInterface
24
{
25
    /**
26
     * Query handlers map.
27
     *
28
     * @var mixed[]
29
     */
30
    protected $handlersMap;
31
32
    /**
33
     * QueryHandlerLocator 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 InvalidQueryHandlerException
46
     */
47
    public function getHandlers(Envelope $envelope): iterable
48
    {
49
        $seen = [];
50
51
        foreach ($this->getQueryMap($envelope) as $type) {
52
            foreach ($this->handlersMap[$type] ?? [] as $alias => $handler) {
53
                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...
54
                    throw new InvalidQueryHandlerException(\sprintf(
55
                        'Query handler must implement %s interface, %s given',
56
                        QueryHandler::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 InvalidQueryException
74
     *
75
     * @return mixed[]
76
     */
77
    final protected function getQueryMap(Envelope $envelope): array
78
    {
79
        $query = $envelope->getMessage();
80
81
        if (!$query instanceof Query) {
0 ignored issues
show
Bug introduced by
The class Gears\CQRS\Query 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...
82
            throw new InvalidQueryException(\sprintf(
83
                'Query must implement %s interface, %s given',
84
                Query::class,
85
                \is_object($query) ? \get_class($query) : \gettype($query)
86
            ));
87
        }
88
89
        $class = \get_class($query);
90
91
        return [$class => $class]
92
            + ['*' => '*'];
93
    }
94
}
95