EntityMapBuilder::buildListEntityInterfaces()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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