Completed
Pull Request — master (#951)
by David
01:48
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
        unset($this->instances[$className]);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function register($object)
48
    {
49
        if (! is_object($object)) {
50
            throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object)));
51
        }
52
53
        $className = $this->normalizeClassName(get_class($object));
54
55
        $this->instances[$className] = $object;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function registerService($className, $serviceId)
62
    {
63
        $this->serviceIds[$this->normalizeClassName($className)] = $serviceId;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function resolve($className)
70
    {
71
        $className = $this->normalizeClassName($className);
72
73
        if (! isset($this->instances[$className])) {
74
            if (isset($this->serviceIds[$className])) {
75
                $this->instances[$className] = $this->resolveService($this->serviceIds[$className]);
76
            } else {
77
                $this->instances[$className] = new $className();
78
            }
79
        }
80
81
        return $this->instances[$className];
82
    }
83
84
    /**
85
     * @return object
86
     */
87
    private function resolveService(string $serviceId)
88
    {
89
        if (! $this->container->has($serviceId)) {
90
            throw new RuntimeException(sprintf('There is no service named "%s"', $serviceId));
91
        }
92
93
        return $this->container->get($serviceId);
94
    }
95
96
    private function normalizeClassName(string $className) : string
97
    {
98
        return trim($className, '\\');
99
    }
100
}
101