Completed
Pull Request — master (#844)
by Christian
03:19
created

LazyEventDispatcher::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 2
nop 1
crap 4.128
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer\EventDispatcher;
20
21
use Psr\Container\ContainerInterface as PsrContainerInterface;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
24
class LazyEventDispatcher extends EventDispatcher
25
{
26
    private $container;
27
28 14
    public function __construct($container)
29
    {
30 14
        if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) {
31
            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)));
32
        }
33
34 14
        $this->container = $container;
35 14
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 12
    protected function initializeListeners($eventName, $loweredClass, $format)
41
    {
42 12
        $listeners = parent::initializeListeners($eventName, $loweredClass, $format);
43
44 12
        foreach ($listeners as &$listener) {
45 12
            if (!is_array($listener) || !is_string($listener[0])) {
46 8
                continue;
47
            }
48
49 4
            if (!$this->container->has($listener[0])) {
50
                continue;
51
            }
52
53 4
            $listener[0] = $this->container->get($listener[0]);
54 12
        }
55
56 12
        return $listeners;
57
    }
58
}
59