Failed Conditions
Push — master ( 952e9b...a46388 )
by Jonathan
36s queued 11s
created

AbstractManagerRegistry::getConnection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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