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

LazyEventDispatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 35
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
B initializeListeners() 0 18 5
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