ContainerAwareCommandBus   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 53
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A handle() 0 12 3
A initialize() 0 15 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\CommandBusBundle;
5
6
use Innmind\CommandBusBundle\Exception\InvalidArgumentException;
7
use Innmind\CommandBus\{
8
    CommandBusInterface,
9
    CommandBus,
10
    Exception\InvalidArgumentException as InvalidCommandException
11
};
12
use Innmind\Immutable\{
13
    Map,
14
    MapInterface
15
};
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
final class ContainerAwareCommandBus implements CommandBusInterface
19
{
20
    private $container;
21
    private $handlers;
22
    private $bus;
23
24 6
    public function __construct(
25
        ContainerInterface $container,
26
        MapInterface $handlers
27
    ) {
28
        if (
29 6
            (string) $handlers->keyType() !== 'string' ||
30 6
            (string) $handlers->valueType() !== 'string'
31
        ) {
32 1
            throw new InvalidArgumentException;
33
        }
34
35 5
        $this->container = $container;
36 5
        $this->handlers = $handlers;
37 5
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    public function handle($command)
43
    {
44 3
        if (!is_object($command)) {
45 1
            throw new InvalidCommandException;
46
        }
47
48 2
        if (!$this->bus instanceof CommandBusInterface) {
49 2
            $this->initialize();
50
        }
51
52 2
        $this->bus->handle($command);
53 2
    }
54
55 2
    private function initialize()
56
    {
57
        $handlers = $this
58 2
            ->handlers
59 2
            ->reduce(
60 2
                new Map('string', 'callable'),
61 2
                function(Map $carry, string $class, string $service): Map {
62 2
                    return $carry->put(
63
                        $class,
1 ignored issue
show
Documentation introduced by
$class is of type string, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64 2
                        $this->container->get($service)
65
                    );
66 2
                }
67
            );
68 2
        $this->bus = new CommandBus($handlers);
69 2
    }
70
}
71