Failed Conditions
Pull Request — master (#2)
by Jonathan
02:45
created

AbstractManagerRegistry   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Test Coverage

Coverage 53.13%

Importance

Changes 0
Metric Value
wmc 26
dl 0
loc 220
rs 10
c 0
b 0
f 0
ccs 34
cts 64
cp 0.5313

13 Methods

Rating   Name   Duplication   Size   Complexity  
B getManagerForClass() 0 25 6
A resetManager() 0 15 3
A getConnection() 0 11 3
A getConnectionNames() 0 3 1
A getConnections() 0 8 2
A getManagerNames() 0 3 1
A __construct() 0 8 1
A getManager() 0 11 3
A getManagers() 0 8 2
A getName() 0 3 1
A getDefaultConnectionName() 0 3 1
A getRepository() 0 3 1
A getDefaultManagerName() 0 3 1
1
<?php
2
namespace Doctrine\Common\Persistence;
3
4
use function explode;
5
use function sprintf;
6
use function strpos;
7
8
/**
9
 * Abstract implementation of the ManagerRegistry contract.
10
 *
11
 * @link   www.doctrine-project.org
12
 */
13
abstract class AbstractManagerRegistry implements ManagerRegistry
14
{
15
    /** @var string */
16
    private $name;
17
18
    /** @var string[] */
19
    private $connections;
20
21
    /** @var string[] */
22
    private $managers;
23
24
    /** @var string */
25
    private $defaultConnection;
26
27
    /** @var string */
28
    private $defaultManager;
29
30
    /** @var string */
31
    private $proxyInterfaceName;
32
33
    /**
34
     * @param string   $name
35
     * @param string[] $connections
36
     * @param string[] $managers
37
     * @param string   $defaultConnection
38
     * @param string   $defaultManager
39
     * @param string   $proxyInterfaceName
40
     */
41 6
    public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
42
    {
43 6
        $this->name               = $name;
44 6
        $this->connections        = $connections;
45 6
        $this->managers           = $managers;
46 6
        $this->defaultConnection  = $defaultConnection;
47 6
        $this->defaultManager     = $defaultManager;
48 6
        $this->proxyInterfaceName = $proxyInterfaceName;
49 6
    }
50
51
    /**
52
     * Fetches/creates the given services.
53
     *
54
     * A service in this context is connection or a manager instance.
55
     *
56
     * @param string $name The name of the service.
57
     *
58
     * @return object The instance of the given service.
59
     */
60
    abstract protected function getService($name);
61
62
    /**
63
     * Resets the given services.
64
     *
65
     * A service in this context is connection or a manager instance.
66
     *
67
     * @param string $name The name of the service.
68
     *
69
     * @return void
70
     */
71
    abstract protected function resetService($name);
72
73
    /**
74
     * Gets the name of the registry.
75
     *
76
     * @return string
77
     */
78
    public function getName()
79
    {
80
        return $this->name;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getConnection($name = null)
87
    {
88
        if ($name === null) {
89
            $name = $this->defaultConnection;
90
        }
91
92
        if (! isset($this->connections[$name])) {
93
            throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
94
        }
95
96
        return $this->getService($this->connections[$name]);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getConnectionNames()
103
    {
104
        return $this->connections;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getConnections()
111
    {
112
        $connections = [];
113
        foreach ($this->connections as $name => $id) {
114
            $connections[$name] = $this->getService($id);
115
        }
116
117
        return $connections;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getDefaultConnectionName()
124
    {
125
        return $this->defaultConnection;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getDefaultManagerName()
132
    {
133
        return $this->defaultManager;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     *
139
     * @throws \InvalidArgumentException
140
     */
141 1
    public function getManager($name = null)
142
    {
143 1
        if ($name === null) {
144 1
            $name = $this->defaultManager;
145
        }
146
147 1
        if (! isset($this->managers[$name])) {
148
            throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
149
        }
150
151 1
        return $this->getService($this->managers[$name]);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 5
    public function getManagerForClass($class)
158
    {
159
        // Check for namespace alias
160 5
        if (strpos($class, ':') !== false) {
161 3
            list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
162 3
            $class                                  = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
163
        }
164
165 5
        $proxyClass = new \ReflectionClass($class);
166
167 3
        if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
168 3
            $parentClass = $proxyClass->getParentClass();
169
170 3
            if (! $parentClass) {
0 ignored issues
show
introduced by
$parentClass is of type ReflectionClass, thus it always evaluated to true.
Loading history...
171 1
                return null;
172
            }
173
174 2
            $class = $parentClass->getName();
175
        }
176
177 2
        foreach ($this->managers as $id) {
178 2
            $manager = $this->getService($id);
179
180 2
            if (! $manager->getMetadataFactory()->isTransient($class)) {
181 2
                return $manager;
182
            }
183
        }
184 2
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getManagerNames()
190
    {
191
        return $this->managers;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getManagers()
198
    {
199
        $dms = [];
200
        foreach ($this->managers as $name => $id) {
201
            $dms[$name] = $this->getService($id);
202
        }
203
204
        return $dms;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function getRepository($persistentObjectName, $persistentManagerName = null)
211
    {
212
        return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 1
    public function resetManager($name = null)
219
    {
220 1
        if ($name === null) {
221 1
            $name = $this->defaultManager;
222
        }
223
224 1
        if (! isset($this->managers[$name])) {
225
            throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
226
        }
227
228
        // force the creation of a new document manager
229
        // if the current one is closed
230 1
        $this->resetService($this->managers[$name]);
231
232 1
        return $this->getManager($name);
233
    }
234
}
235