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

ContainerAwareCommandHandlerLocator::getHandlers()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
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\CommandHandler;
17
use Gears\CQRS\Symfony\Messenger\Exception\InvalidCommandHandlerException;
18
use Psr\Container\ContainerInterface;
19
use Symfony\Component\Messenger\Envelope;
20
21
class ContainerAwareCommandHandlerLocator extends CommandHandlerLocator
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 InvalidCommandHandlerException
47
     */
48
    public function getHandlers(Envelope $envelope): iterable
49
    {
50
        $seen = [];
51
52
        foreach ($this->getCommandMap($envelope) as $type) {
53
            foreach ($this->handlersMap[$type] ?? [] as $alias => $handler) {
54
                $handler = $this->container->get($handler);
55
56
                if (!$handler instanceof CommandHandler) {
0 ignored issues
show
Bug introduced by
The class Gears\CQRS\CommandHandler 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 InvalidCommandHandlerException(\sprintf(
58
                        'Command handler must implement %s interface, %s given',
59
                        CommandHandler::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