Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
created

AbstractManagerRegistry   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 41.79%

Importance

Changes 0
Metric Value
wmc 26
dl 0
loc 225
rs 10
c 0
b 0
f 0
ccs 28
cts 67
cp 0.4179

13 Methods

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