Passed
Push — types ( 30d5cd )
by Jonathan
04:47
created

getDefaultConnectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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