Completed
Pull Request — master (#951)
by David
02:26
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
    private $container;
12
13
    /** @var object[] Map to store entity listener instances. */
14
    private $instances = [];
15
16
    /** @var string[] Map to store registered service ids */
17
    private $serviceIds = [];
18
19
    /**
20
     * @param ContainerInterface $container a service locator for listeners
21
     */
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function clear($className = null)
31
    {
32
        if ($className === null) {
33
            $this->instances = [];
34
35
            return;
36
        }
37
38
        $className = $this->normalizeClassName($className);
39
40
        unset($this->instances[$className]);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function register($object)
47
    {
48
        if (! is_object($object)) {
49
            throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object)));
50
        }
51
52
        $className = $this->normalizeClassName(get_class($object));
53
54
        $this->instances[$className] = $object;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function registerService($className, $serviceId)
61
    {
62
        $this->serviceIds[$this->normalizeClassName($className)] = $serviceId;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function resolve($className)
69
    {
70
        $className = $this->normalizeClassName($className);
71
72
        if (! isset($this->instances[$className])) {
73
            if (isset($this->serviceIds[$className])) {
74
                $this->instances[$className] = $this->resolveService($this->serviceIds[$className]);
75
            } else {
76
                $this->instances[$className] = new $className();
77
            }
78
        }
79
80
        return $this->instances[$className];
81
    }
82
83
    /**
84
     * @return object
85
     */
86
    private function resolveService(string $serviceId)
87
    {
88
        if (! $this->container->has($serviceId)) {
89
            throw new RuntimeException(sprintf('There is no service named "%s"', $serviceId));
90
        }
91
92
        return $this->container->get($serviceId);
93
    }
94
95
    private function normalizeClassName(string $className): string
96
    {
97
        return trim($className, '\\');
98
    }
99
}
100