Failed Conditions
Pull Request — master (#1)
by Jonathan
13:22 queued 10:46
created

AbstractManagerRegistry::getManagers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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