Completed
Pull Request — master (#443)
by Peter
05:01
created

resolveService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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