Completed
Pull Request — master (#951)
by David
01:53
created

ContainerEntityListenerResolver::resolveService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
4
5
use InvalidArgumentException;
6
use Psr\Container\ContainerInterface;
7
use RuntimeException;
8
9
class ContainerEntityListenerResolver implements EntityListenerServiceResolver
10
{
11
    /** @var ContainerInterface */
12
    private $container;
13
14
    /** @var object[] Map to store entity listener instances. */
15
    private $instances = [];
16
17
    /** @var string[] Map to store registered service ids */
18
    private $serviceIds = [];
19
20
    /**
21
     * @param ContainerInterface $container a service locator for listeners
22
     */
23
    public function __construct(ContainerInterface $container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function clear($className = null)
32
    {
33
        if ($className === null) {
34
            $this->instances = [];
35
36
            return;
37
        }
38
39
        $className = $this->normalizeClassName($className);
40
41
        if (! isset($this->instances[$className])) {
42
            return;
43
        }
44
45
        unset($this->instances[$className]);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function register($object)
52
    {
53
        if (! is_object($object)) {
54
            throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object)));
55
        }
56
57
        $className = $this->normalizeClassName(get_class($object));
58
59
        $this->instances[$className] = $object;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function registerService($className, $serviceId)
66
    {
67
        $this->serviceIds[$this->normalizeClassName($className)] = $serviceId;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function resolve($className)
74
    {
75
        $className = $this->normalizeClassName($className);
76
77
        if (! isset($this->instances[$className])) {
78
            if (isset($this->serviceIds[$className])) {
79
                $this->instances[$className] = $this->resolveService($this->serviceIds[$className]);
80
            } else {
81
                $this->instances[$className] = new $className();
82
            }
83
        }
84
85
        return $this->instances[$className];
86
    }
87
88
    /**
89
     * @param string $serviceId
90
     *
91
     * @return object
92
     */
93
    private function resolveService($serviceId)
94
    {
95
        if (! $this->container->has($serviceId)) {
96
            throw new RuntimeException(sprintf('There is no service named "%s"', $serviceId));
97
        }
98
99
        return $this->container->get($serviceId);
100
    }
101
102
    /**
103
     * @param string $className
104
     *
105
     * @return string
106
     */
107
    private function normalizeClassName($className)
108
    {
109
        return trim($className, '\\');
110
    }
111
}
112