Completed
Push — master ( 5c0b6f...5c71f2 )
by Oleg
10:16
created

SimpleRegistry::getManager()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 6
cts 9
cp 0.6667
rs 9.7333
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4.5923
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Doctrine;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityManagerInterface;
11
12
final class SimpleRegistry implements ManagerRegistry
13
{
14
    const DEFAULT_CONNECTION_NAME = 'default';
15
    const DEFAULT_MANAGER_NAME = 'default';
16
17
    /**
18
     * @var Connection[]
19
     */
20
    private $connections = [];
21
    /**
22
     * @var EntityManagerInterface[]
23
     */
24
    private $managers = [];
25
    /**
26
     * Custom Entity=>Manager config
27
     * @var array
28
     */
29
    private $entityManagerMapping = [];
30
31 58
    public function __construct(
32
        array $connections,
33
        array $managers,
34
        array $entityManagerMapping = []
35
    ) {
36 58
        $this->connections = $connections;
37 58
        $this->managers = $managers;
38 58
        $this->entityManagerMapping = $entityManagerMapping;
39 58
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getDefaultConnectionName(): string
45
    {
46
        return self::DEFAULT_CONNECTION_NAME;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function getConnection($name = null): Connection
53
    {
54
        if ($name === null) {
55
            $name = $this->getDefaultConnectionName();
56
        }
57
58
        if (isset($this->connections[$name])) {
59
            return $this->connections[$name];
60
        }
61
62
        throw new \InvalidArgumentException(sprintf('Could not find Doctrine connection with name %s', $name));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     * @return Connection[]
68
     */
69
    public function getConnections(): array
70
    {
71
        return $this->connections;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getConnectionNames(): array
78
    {
79
        return array_keys($this->connections);
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 57
    public function getDefaultManagerName(): string
86
    {
87 57
        return self::DEFAULT_MANAGER_NAME;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * @throws \Doctrine\ORM\ORMException
93
     */
94 57
    public function getManager($name = null): EntityManagerInterface
95
    {
96 57
        if ($name === null) {
97
            $name = $this->getDefaultManagerName();
98
        }
99
100 57
        if (isset($this->managers[$name])) {
101 57
            $manager = $this->managers[$name];
102 57
            if (!$manager->isOpen()) {
103
                return $this->resetManager($name);
104
            }
105 57
            return $manager;
106
        }
107
108
        throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name));
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     * @return EntityManagerInterface[]
114
     */
115
    public function getManagers(): array
116
    {
117
        return $this->managers;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     * @throws \Doctrine\ORM\ORMException
123
     */
124
    public function resetManager($name = null): EntityManagerInterface
125
    {
126
        if ($name === null) {
127
            $name = $this->getDefaultManagerName();
128
        }
129
130
        if (!isset($this->managers[$name])) {
131
            throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name));
132
        }
133
134
        $manager = $this->managers[$name];
135
        $this->managers[$name] = EntityManager::create(
136
            $manager->getConnection(),
137
            $manager->getConfiguration(),
138
            $manager->getEventManager()
139
        );
140
141
        return $this->getManager($name);
142
    }
143
144
    /**
145
     * @inheritdoc
146
     * @unsupported
147
     */
148
    public function getAliasNamespace($alias): string
149
    {
150
        return $alias;
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function getManagerNames(): array
157
    {
158
        return array_keys($this->managers);
159
    }
160
161
    /**
162
     * @inheritdoc
163
     * @throws \Doctrine\ORM\ORMException
164
     */
165 57
    public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository
166
    {
167 57
        if ($persistentManagerName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $persistentManagerName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
168
            $manager = $this->getManager($persistentManagerName);
169
        } else {
170 57
            $manager = $this->getManagerForClass($persistentObject);
171
        }
172 57
        return $manager->getRepository($persistentObject);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     * @throws \Doctrine\ORM\ORMException
178
     */
179 57
    public function getManagerForClass($class): EntityManagerInterface
180
    {
181 57
        $name = $this->entityManagerMapping[$class] ?? $this->getDefaultManagerName();
182
183 57
        return $this->getManager($name);
184
    }
185
}
186