LazyEventDispatcher::initializeListeners()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 17
ccs 4
cts 5
cp 0.8
crap 5.2
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\EventDispatcher;
6
7
use JMS\Serializer\Exception\InvalidArgumentException;
8
use Psr\Container\ContainerInterface as PsrContainerInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class LazyEventDispatcher extends EventDispatcher
12
{
13
    /**
14
     * @var PsrContainerInterface|ContainerInterface
15 18
     */
16
    private $container;
17 18
18
    /**
19
     * @param PsrContainerInterface|ContainerInterface $container
20
     */
21 18
    public function __construct($container)
22 18
    {
23
        if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
0 ignored issues
show
introduced by
$container is always a sub-type of Psr\Container\ContainerInterface.
Loading history...
24
            throw new InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).', PsrContainerInterface::class, ContainerInterface::class, \is_object($container) ? \get_class($container) : \gettype($container)));
25
        }
26
27 16
        $this->container = $container;
28
    }
29 16
30
    /**
31 16
     * {@inheritdoc}
32 16
     */
33 12
    protected function initializeListeners(string $eventName, string $loweredClass, string $format): array
34
    {
35
        $listeners = parent::initializeListeners($eventName, $loweredClass, $format);
36 4
37
        foreach ($listeners as &$listener) {
38
            if (!\is_array($listener[0]) || !\is_string($listener[0][0])) {
39
                continue;
40 4
            }
41
42
            if (!$this->container->has($listener[0][0])) {
43 16
                continue;
44
            }
45
46
            $listener[0][0] = $this->container->get($listener[0][0]);
47
        }
48
49
        return $listeners;
50
    }
51
}
52