getDefaultConnectionName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\DoctrineDbServiceProvider\Registry;
6
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Doctrine\Common\Persistence\ObjectManager;
9
use Doctrine\Common\Persistence\ObjectRepository;
10
use Doctrine\Common\Persistence\Proxy;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Doctrine\ORM\EntityRepository;
14
use Doctrine\ORM\ORMException;
15
use Pimple\Container;
16
17
final class DoctrineOrmManagerRegistry implements ManagerRegistry
18
{
19
    /**
20
     * @var Container
21
     */
22
    private $container;
23
24
    /**
25
     * @var Container
26
     */
27
    private $connections;
28
29
    /**
30
     * @var array<int, string>
31
     */
32
    private $connectionNames;
33
34
    /**
35
     * @var string
36
     */
37
    private $defaultConnectionName;
38
39
    /**
40
     * @var Container
41
     */
42
    private $originalEntityManagers;
43
44
    /**
45
     * @var array<string, EntityManagerInterface>
46
     */
47
    private $resetedManagers = [];
48
49 22
    /**
50
     * @var array<int, string>
51 22
     */
52 22
    private $managerNames;
53
54 3
    /**
55
     * @var string
56 3
     */
57
    private $defaultManagerName;
58 3
59
    public function __construct(Container $container)
60
    {
61
        $this->container = $container;
62
    }
63
64
    public function getDefaultConnectionName(): string
65
    {
66
        $this->loadConnections();
67
68 3
        return $this->defaultConnectionName;
69
    }
70 3
71
    /**
72 3
     * @param string|null $name
73
     *
74 3
     * @throws \InvalidArgumentException
75 1
     */
76
    public function getConnection($name = null): Connection
77
    {
78 2
        $this->loadConnections();
79
80
        $name = $name ?? $this->getDefaultConnectionName();
81
82
        if (!isset($this->connections[$name])) {
83
            throw new \InvalidArgumentException(sprintf('Missing connection with name "%s".', $name));
84 1
        }
85
86 1
        return $this->connections[$name];
87
    }
88 1
89
    /**
90 1
     * @return array<string, Connection>
91
     */
92 1
    public function getConnections(): array
93 1
    {
94
        $this->loadConnections();
95
96 1
        $connections = [];
97
        /** @var string $name */
98
        foreach ($this->connectionNames as $name) {
99
            /** @var Connection $connection */
100
            $connection = $this->connections[$name];
101
            $connections[$name] = $connection;
102 1
        }
103
104 1
        return $connections;
105
    }
106 1
107
    /**
108
     * @return array<string>
109 6
     */
110
    public function getConnectionNames(): array
111 6
    {
112
        $this->loadConnections();
113 6
114
        return $this->connectionNames;
115
    }
116
117
    public function getDefaultManagerName(): string
118
    {
119
        $this->loadManagers();
120
121 9
        return $this->defaultManagerName;
122
    }
123 9
124
    /**
125 9
     * @param string|null $name
126
     *
127 9
     * @return EntityManagerInterface|ObjectManager
128 1
     */
129
    public function getManager($name = null): ObjectManager
130
    {
131 8
        $this->loadManagers();
132 1
133
        $name = $name ?? $this->getDefaultManagerName();
134
135 7
        if (!isset($this->originalEntityManagers[$name])) {
136
            throw new \InvalidArgumentException(sprintf('Missing manager with name "%s".', $name));
137
        }
138
139
        if (isset($this->resetedManagers[$name])) {
140
            return $this->resetedManagers[$name];
141 2
        }
142
143 2
        return $this->originalEntityManagers[$name];
144
    }
145 2
146
    /**
147 2
     * @return array<string, EntityManagerInterface>|array<string, ObjectManager>
148
     */
149 2
    public function getManagers(): array
150 1
    {
151
        $this->loadManagers();
152 1
153
        $entityManagers = [];
154
        /** @var string $name */
155 2
        foreach ($this->managerNames as $name) {
156
            /* @var EntityManagerInterface $entityManager */
157
            if (isset($this->resetedManagers[$name])) {
158 2
                $entityManager = $this->resetedManagers[$name];
159
            } else {
160
                $entityManager = $this->originalEntityManagers[$name];
161
            }
162
163
            $entityManagers[$name] = $entityManager;
164 5
        }
165
166 5
        return $entityManagers;
167
    }
168 5
169
    /**
170
     * @return array<string>
171
     */
172
    public function getManagerNames(): array
173
    {
174
        $this->loadManagers();
175
176 3
        return $this->managerNames;
177
    }
178 3
179
    /**
180 3
     * @param string|null $name
181
     *
182 3
     * @return EntityManagerInterface|ObjectManager
183 1
     */
184
    public function resetManager($name = null)
185
    {
186
        $this->loadManagers();
187 2
188
        $name = $name ?? $this->getDefaultManagerName();
189
190 2
        if (!isset($this->originalEntityManagers[$name])) {
191
            throw new \InvalidArgumentException(sprintf('Missing manager with name "%s".', $name));
192
        }
193 2
194 2
        /** @var EntityManagerInterface $originalEntityManager */
195 2
        $originalEntityManager = $this->originalEntityManagers[$name];
196 2
197
        /** @var callable $entityManagerFactory */
198
        $entityManagerFactory = $this->container['doctrine.orm.em.factory'];
199 2
200
        /** @var EntityManagerInterface $entityManager */
201 2
        $entityManager = $entityManagerFactory(
202
            $originalEntityManager->getConnection(),
203
            $originalEntityManager->getConfiguration(),
204
            $originalEntityManager->getEventManager()
205
        );
206
207
        $this->resetedManagers[$name] = $entityManager;
208
209
        return $entityManager;
210
    }
211 2
212
    /**
213 2
     * @param string $alias
214
     *
215
     * @throws ORMException
216 2
     */
217
    public function getAliasNamespace($alias): string
218 2
    {
219 1
        foreach ($this->getManagerNames() as $name) {
220
            try {
221
                /** @var EntityManagerInterface $entityManager */
222
                $entityManager = $this->getManager($name);
223 1
224
                return $entityManager->getConfiguration()->getEntityNamespace($alias);
225
            } catch (ORMException $exception) {
226
                // throw the exception only if no manager can solve it
227
            }
228
        }
229
        throw ORMException::unknownEntityNamespace($alias);
230
    }
231
232 1
    /**
233
     * @param string      $persistentObject
234 1
     * @param string|null $persistentManagerName
235
     *
236
     * @return EntityRepository|ObjectRepository
237
     */
238
    public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository
239
    {
240
        return $this->getManager($persistentManagerName)->getRepository($persistentObject);
241
    }
242 2
243
    /**
244 2
     * @param string $class
245 2
     *
246
     * @return EntityManagerInterface|ObjectManager|null
247 2
     */
248 2
    public function getManagerForClass($class)
249
    {
250
        $reflectionClass = new \ReflectionClass($class);
251 2
        if ($reflectionClass->implementsInterface(Proxy::class)) {
252 2
            /** @var \ReflectionClass $reflectionParentClass */
253 2
            $reflectionParentClass = $reflectionClass->getParentClass();
254 2
            $class = $reflectionParentClass->getName();
255
        }
256
257 1
        foreach ($this->getManagerNames() as $name) {
258
            $entityManager = $this->getManager($name);
259 6
            if (!$entityManager->getMetadataFactory()->isTransient($class)) {
260
                return $entityManager;
261 6
            }
262 6
        }
263 6
    }
264
265 6
    private function loadConnections(): void
266
    {
267 16
        if (null === $this->connections) {
268
            $this->connections = $this->container['doctrine.dbal.dbs'];
269 16
            $this->connectionNames = $this->container['doctrine.dbal.dbs.name'];
270 16
            $this->defaultConnectionName = $this->container['doctrine.dbal.dbs.default'];
271 16
        }
272
    }
273 16
274
    private function loadManagers(): void
275
    {
276
        if (null === $this->originalEntityManagers) {
277
            $this->originalEntityManagers = $this->container['doctrine.orm.ems'];
278
            $this->managerNames = $this->container['doctrine.orm.ems.name'];
279
            $this->defaultManagerName = $this->container['doctrine.orm.ems.default'];
280
        }
281
    }
282
}
283