Completed
Push — dev ( 867b7a...526cfa )
by Андрей
02:26
created

EntityMapBuilder::getDoctrineObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\Utils;
7
8
use Interop\Container\ContainerInterface;
9
use Doctrine\Common\Persistence\ObjectManager;
10
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
11
use Nnx\Doctrine\EntityManager\EntityManagerInterface;
12
use ReflectionClass;
13
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
14
15
/**
16
 * Class EntityMapBuilder
17
 *
18
 * @package Nnx\Doctrine\Utils
19
 */
20
class EntityMapBuilder implements EntityMapBuilderInterface
21
{
22
23
    /**
24
     * Плагин менеджер для получения ObjectManager'ов Doctrine2
25
     *
26
     * @var ContainerInterface
27
     */
28
    protected $doctrineObjectManager;
29
30
    /**
31
     * Менеджер для работы с настройками модуля
32
     *
33
     * @var ModuleOptionsPluginManagerInterface
34
     */
35
    protected $moduleOptionsPluginManager;
36
37
    /**
38
     * Менеджер для получения сущностей Doctrine
39
     *
40
     * @var EntityManagerInterface
41
     */
42
    protected $entityManager;
43
44
    /**
45
     * EntityMapBuilder constructor.
46
     *
47
     * @param ContainerInterface                  $doctrineObjectManager
48
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager
49
     * @param EntityManagerInterface              $entityManager
50
     */
51
    public function __construct(
52
        ContainerInterface $doctrineObjectManager,
53
        ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager,
54
        EntityManagerInterface $entityManager
55
    ) {
56
        $this->setDoctrineObjectManager($doctrineObjectManager);
57
        $this->setModuleOptionsPluginManager($moduleOptionsPluginManager);
58
        $this->setEntityManager($entityManager);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     *
64
     * @param $objectManagerName
65
     *
66
     * @return array
67
     *
68
     * @throws \Interop\Container\Exception\NotFoundException
69
     * @throws \Interop\Container\Exception\ContainerException
70
     */
71
    public function buildEntityMapByObjectManagerName($objectManagerName)
72
    {
73
        $entityClassNames = $this->getEntityClassNamesByObjectManagerName($objectManagerName);
74
75
        $listEntityInterfaces = $this->buildListEntityInterfaces($entityClassNames);
76
77
        $map = [];
78
79
        $entityManager = $this->getEntityManager();
80
        foreach ($listEntityInterfaces as $currentInterface) {
81
            if ($entityManager->hasEntityClassByInterface($currentInterface)) {
82
                $map[$currentInterface] = $entityManager->get($currentInterface);
83
            }
84
        }
85
86
        return $map;
87
    }
88
89
    /**
90
     * Получает список интерфейсов используемых в сущностях
91
     *
92
     * @param array $entityClassNames
93
     *
94
     * @return array
95
     */
96
    public function buildListEntityInterfaces(array $entityClassNames = [])
97
    {
98
        $listEntityInterfaces = [];
99
100
        $moduleOptionsManager = $this->getModuleOptionsPluginManager();
101
        $allowedModules = $this->getAllowedModules($entityClassNames);
102
103
        foreach ($entityClassNames as $entityClassName) {
104
            $r = new ReflectionClass($entityClassName);
105
            $useInterfaces = $r->getInterfaceNames();
106
            foreach ($useInterfaces as $currentInterface) {
107
                if ($moduleOptionsManager->hasModuleNameByClassName($currentInterface)) {
108
                    $moduleNameByInterface = $moduleOptionsManager->getModuleNameByClassName($currentInterface);
109
                    if (array_key_exists($moduleNameByInterface, $allowedModules)) {
110
                        $listEntityInterfaces[$currentInterface] = $currentInterface;
111
                    }
112
                }
113
            }
114
        }
115
116
        return $listEntityInterfaces;
117
    }
118
119
    /**
120
     * Подготавливает список модулей, в которых могут находиться интерфейсы сущностей
121
     *
122
     * @param array $entityClassNames
123
     *
124
     * @return array
125
     */
126
    public function getAllowedModules(array $entityClassNames = [])
127
    {
128
        $moduleOptionsManager = $this->getModuleOptionsPluginManager();
129
        $allowedModules = [];
130
        foreach ($entityClassNames as $entityClassName) {
131
            if ($moduleOptionsManager->hasModuleNameByClassName($entityClassName)) {
132
                $moduleName = $moduleOptionsManager->getModuleNameByClassName($entityClassName);
133
                $allowedModules[$moduleName] = $moduleName;
134
            }
135
        }
136
137
        return  $allowedModules;
138
    }
139
140
    /**
141
     * Получает список сущностей для ObjectManager'a
142
     *
143
     * @param $objectManagerName
144
     *
145
     * @return array
146
     *
147
     * @throws \Interop\Container\Exception\NotFoundException
148
     * @throws \Interop\Container\Exception\ContainerException
149
     */
150
    public function getEntityClassNamesByObjectManagerName($objectManagerName)
151
    {
152
        /** @var ObjectManager $objectManager */
153
        $objectManager = $this->getDoctrineObjectManager()->get($objectManagerName);
154
155
        $allMetadata = $objectManager->getMetadataFactory()->getAllMetadata();
156
157
        $entityClassNames = [];
158
        foreach ($allMetadata as $classMetadata) {
159
            if ($classMetadata instanceof ClassMetadata) {
160
                $entityClassName = $classMetadata->getName();
161
                $entityClassNames[$entityClassName] = $entityClassName;
162
            }
163
        }
164
165
        return $entityClassNames;
166
    }
167
168
169
170
171
    /**
172
     * Возвращает плагин менеджер для получения ObjectManager'ов Doctrine2
173
     *
174
     * @return ContainerInterface
175
     */
176
    public function getDoctrineObjectManager()
177
    {
178
        return $this->doctrineObjectManager;
179
    }
180
181
    /**
182
     * Устанавливает плагин менеджер для получения ObjectManager'ов Doctrine2
183
     *
184
     * @param ContainerInterface $doctrineObjectManager
185
     *
186
     * @return $this
187
     */
188
    public function setDoctrineObjectManager(ContainerInterface $doctrineObjectManager)
189
    {
190
        $this->doctrineObjectManager = $doctrineObjectManager;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Возвращает менеджер для работы с настройками модуля
197
     *
198
     * @return ModuleOptionsPluginManagerInterface
199
     */
200
    public function getModuleOptionsPluginManager()
201
    {
202
        return $this->moduleOptionsPluginManager;
203
    }
204
205
    /**
206
     * Устанавливает менеджер для работы с настройками модуля
207
     *
208
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager
209
     *
210
     * @return $this
211
     */
212
    public function setModuleOptionsPluginManager(ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager)
213
    {
214
        $this->moduleOptionsPluginManager = $moduleOptionsPluginManager;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Возвращает менеджер для получения сущностей Doctrine
221
     *
222
     * @return EntityManagerInterface
223
     */
224
    public function getEntityManager()
225
    {
226
        return $this->entityManager;
227
    }
228
229
    /**
230
     * Устанавливает менеджер для получения сущностей Doctrine
231
     *
232
     * @param EntityManagerInterface $entityManager
233
     *
234
     * @return $this
235
     */
236
    public function setEntityManager(EntityManagerInterface $entityManager)
237
    {
238
        $this->entityManager = $entityManager;
239
240
        return $this;
241
    }
242
}
243