ContainerAwareEventBus::dispatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\EventBusBundle;
5
6
use Innmind\EventBusBundle\Exception\InvalidArgumentException;
7
use Innmind\EventBus\{
8
    EventBusInterface,
9
    EventBus,
10
    Exception\InvalidArgumentException as InvalidEventException,
11
    ClassName\ExtractorInterface
12
};
13
use Innmind\Immutable\{
14
    Map,
15
    MapInterface,
16
    SetInterface,
17
    Set
18
};
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
final class ContainerAwareEventBus implements EventBusInterface
22
{
23
    private $container;
24
    private $listeners;
25
    private $extractor;
26
    private $bus;
27
28 6
    public function __construct(
29
        ContainerInterface $container,
30
        MapInterface $listeners,
31
        ExtractorInterface $extractor
32
    ) {
33
        if (
34 6
            (string) $listeners->keyType() !== 'string' ||
35 6
            (string) $listeners->valueType() !== SetInterface::class
36
        ) {
37 1
            throw new InvalidArgumentException;
38
        }
39
40
        $listeners->foreach(function(string $class, SetInterface $listeners) {
41 3
            if ((string) $listeners->type() !== 'string') {
42
                throw new InvalidArgumentException;
43
            }
44 5
        });
45
46 5
        $this->container = $container;
47 5
        $this->listeners = $listeners;
48 5
        $this->extractor = $extractor;
49 5
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function dispatch($event): EventBusInterface
55
    {
56 3
        if (!is_object($event)) {
57 1
            throw new InvalidEventException;
58
        }
59
60 2
        if (!$this->bus instanceof EventBusInterface) {
61 2
            $this->initialize();
62
        }
63
64 2
        $this->bus->dispatch($event);
65
66 2
        return $this;
67
    }
68
69 2
    private function initialize()
70
    {
71
        $listeners = $this
72 2
            ->listeners
73 2
            ->reduce(
74 2
                new Map('string', SetInterface::class),
75
                function(Map $carry, string $class, SetInterface $services): Map {
76 2
                    return $carry->put(
77
                        $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...
78 2
                        $services->reduce(
79 2
                            new Set('callable'),
80 2
                            function(Set $carry, string $service): Set {
81 2
                                return $carry->add(
82 2
                                    $this->container->get($service)
83
                                );
84 2
                            }
85
                        )
86
                    );
87 2
                }
88
            );
89 2
        $this->bus = new EventBus($listeners, $this->extractor);
90 2
    }
91
}
92