Completed
Push — master ( 0456c5...25ee32 )
by Oleg
04:45
created

SimpleRegistry::getAliasNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Doctrine;
5
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\EntityManagerInterface;
10
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
11
12
final class SimpleRegistry implements EntityManagerRegistry
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 134
    public function __construct(
32
        array $connections,
33
        array $managers,
34
        array $entityManagerMapping = []
35
    ) {
36 134
        $this->connections = $connections;
37 134
        $this->managers = $managers;
38 134
        $this->entityManagerMapping = $entityManagerMapping;
39 134
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 2
    public function getDefaultConnectionName(): string
45
    {
46 2
        return self::DEFAULT_CONNECTION_NAME;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 2
    public function getConnection($name = null): Connection
53
    {
54 2
        if ($name === null) {
55 2
            $name = $this->getDefaultConnectionName();
56
        }
57
58 2
        if (isset($this->connections[$name])) {
59 2
            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 2
    public function getConnections(): array
70
    {
71 2
        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 136
    public function getDefaultManagerName(): string
86
    {
87 136
        return self::DEFAULT_MANAGER_NAME;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * @throws \Doctrine\ORM\ORMException
93
     */
94 138
    public function getManager($name = null): EntityManagerInterface
95
    {
96 138
        if ($name === null) {
97 2
            $name = $this->getDefaultManagerName();
98
        }
99
100 138
        if (isset($this->managers[$name])) {
101 136
            $manager = $this->managers[$name];
102 136
            if (!$manager->isOpen()) {
103 2
                return $this->resetManager($name);
104
            }
105 136
            return $manager;
106
        }
107
108 2
        throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name));
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     * @return EntityManagerInterface[]
114
     */
115 2
    public function getManagers(): array
116
    {
117 2
        return $this->managers;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     * @throws \Doctrine\ORM\ORMException
123
     */
124 2
    public function resetManager($name = null): EntityManagerInterface
125
    {
126 2
        if ($name === null) {
127
            $name = $this->getDefaultManagerName();
128
        }
129
130 2
        if (!isset($this->managers[$name])) {
131
            throw new \InvalidArgumentException(sprintf('Could not find Doctrine manager with name %s', $name));
132
        }
133
134 2
        $manager = $this->managers[$name];
135 2
        $this->managers[$name] = EntityManager::create(
136 2
            $manager->getConnection(),
137 2
            $manager->getConfiguration(),
138 2
            $manager->getEventManager()
139
        );
140
141 2
        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 134
    public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository
166
    {
167 134
        if ($persistentManagerName !== null) {
168
            $manager = $this->getManager($persistentManagerName);
169
        } else {
170 134
            $manager = $this->getManagerForClass($persistentObject);
171
        }
172 134
        return $manager->getRepository($persistentObject);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     * @throws \Doctrine\ORM\ORMException
178
     */
179 134
    public function getManagerForClass($class): EntityManagerInterface
180
    {
181 134
        $name = $this->entityManagerMapping[$class] ?? $this->getDefaultManagerName();
182
183 134
        return $this->getManager($name);
184
    }
185
}
186