ObjectManagerAutoDetector::hasObjectManagerNameByClassName()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.439
cc 5
eloc 18
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\ObjectManager;
7
8
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
use Interop\Container\ContainerInterface;
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Doctrine\Common\Util\ClassUtils;
12
13
/**
14
 * Class ObjectManagerAutoDetector
15
 *
16
 * @package Nnx\Doctrine\ObjectManager
17
 */
18
class ObjectManagerAutoDetector implements ObjectManagerAutoDetectorInterface
19
{
20
    /**
21
     * Менеджер для работы с настройками модуля
22
     *
23
     * @var ModuleOptionsPluginManagerInterface
24
     */
25
    protected $moduleOptionsManager;
26
27
    /**
28
     * Кеш, ключем являтся имя класса, а значением имя ObjectManager
29
     *
30
     * @var array
31
     */
32
    protected $classNameToObjectManagerName = [];
33
34
    /**
35
     * Плагин менеджер для получения ObjectManager'ов Doctrine2
36
     *
37
     * @var ContainerInterface
38
     */
39
    protected $doctrineObjectManager;
40
41
    /**
42
     * ObjectManagerAutoDetector constructor.
43
     *
44
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
45
     * @param ContainerInterface      $doctrineObjectManager
46
     */
47
    public function __construct(
48
        ModuleOptionsPluginManagerInterface $moduleOptionsManager,
49
        ContainerInterface $doctrineObjectManager
50
    ) {
51
        $this->setModuleOptionsManager($moduleOptionsManager);
52
        $this->setDoctrineObjectManager($doctrineObjectManager);
53
    }
54
55
    /**
56
     * Получает имя используемого в модуле ObjectManager'a Doctrine, по имени любого класса модуля
57
     *
58
     * @param $className
59
     *
60
     * @return string
61
     *
62
     * @throws Exception\ErrorBuildObjectManagerNameException
63
     */
64
    public function getObjectManagerNameByClassName($className)
65
    {
66
        $realClass = ClassUtils::getRealClass($className);
67
        if (false === $this->hasObjectManagerNameByClassName($realClass)) {
68
            $errMsg = sprintf('Failed to get the manager\'s name for class %s', $realClass);
69
            throw new Exception\ErrorBuildObjectManagerNameException($errMsg);
70
        }
71
72
        return $this->classNameToObjectManagerName[$realClass];
73
    }
74
75
76
    /**
77
     * Проверяет есть ли возможность по имени класса модуля, получить имя objectManager'a который используется в данном модуле
78
     *
79
     * @param $className
80
     *
81
     * @return boolean
82
     *
83
     */
84
    public function hasObjectManagerNameByClassName($className)
85
    {
86
        $realClass = ClassUtils::getRealClass($className);
87
88
        if (array_key_exists($realClass, $this->classNameToObjectManagerName)) {
89
            return false !== $this->classNameToObjectManagerName[$realClass];
90
        }
91
92
        $moduleOptionsManager =  $this->getModuleOptionsManager();
93
        if (!$moduleOptionsManager->hasOptionsByClassName($realClass)) {
94
            $this->classNameToObjectManagerName[$realClass] = false;
95
            return false;
96
        }
97
98
        $moduleOptions = $moduleOptionsManager->getOptionsByClassName($realClass);
99
100
        if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) {
101
            $this->classNameToObjectManagerName[$realClass] = false;
102
            return false;
103
        }
104
105
        $objectManagerName = $moduleOptions->getObjectManagerName();
106
107
        if (!is_string($objectManagerName)) {
108
            $this->classNameToObjectManagerName[$realClass] = false;
109
            return false;
110
        }
111
        $this->classNameToObjectManagerName[$realClass] = $objectManagerName;
112
113
        return false !== $this->classNameToObjectManagerName[$realClass];
114
    }
115
116
117
    /**
118
     * Проверяет есть ли возможность по имени класса модуля, получить objectManager'a который используется в данном модуле
119
     *
120
     * @param $className
121
     *
122
     * @return boolean
123
     *
124
     * @throws Exception\ErrorBuildObjectManagerNameException
125
     */
126
    public function hasObjectManagerByClassName($className)
127
    {
128
        $realClass = ClassUtils::getRealClass($className);
129
130
        if ($this->hasObjectManagerNameByClassName($realClass)) {
131
            $objectManagerName = $this->getObjectManagerNameByClassName($realClass);
132
            return $this->getDoctrineObjectManager()->has($objectManagerName);
133
        }
134
        return false;
135
    }
136
137
    /**
138
     * По имени класса модуля, получает objectManager'a который используется в данном модуле
139
     *
140
     * @param $className
141
     *
142
     * @return ObjectManager
143
     *
144
     * @throws Exception\ErrorBuildObjectManagerNameException
145
     * @throws \Interop\Container\Exception\NotFoundException
146
     * @throws \Interop\Container\Exception\ContainerException
147
     * @throws Exception\ErrorBuildObjectManagerException
148
     */
149
    public function getObjectManagerByClassName($className)
150
    {
151
        $realClass = ClassUtils::getRealClass($className);
152
153
        if (!$this->hasObjectManagerByClassName($realClass)) {
154
            $errMsg = sprintf('Failed to get the manager\'s name for class %s', $realClass);
155
            throw new Exception\ErrorBuildObjectManagerException($errMsg);
156
        }
157
        $objectManagerName = $this->getObjectManagerNameByClassName($realClass);
158
159
        return $this->getDoctrineObjectManager()->get($objectManagerName);
160
    }
161
162
163
164
    /**
165
     * Возвращает менеджер для работы с настройками модуля
166
     *
167
     * @return ModuleOptionsPluginManagerInterface
168
     */
169
    public function getModuleOptionsManager()
170
    {
171
        return $this->moduleOptionsManager;
172
    }
173
174
    /**
175
     * Устанавливает менеджер для работы с настройками модуля
176
     *
177
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
178
     *
179
     * @return $this
180
     */
181
    public function setModuleOptionsManager(ModuleOptionsPluginManagerInterface $moduleOptionsManager)
182
    {
183
        $this->moduleOptionsManager = $moduleOptionsManager;
184
185
        return $this;
186
    }
187
188
    /**
189
     * Возрвщает плагин менеджер для получения ObjectManager'ов Doctrine2
190
     *
191
     * @return ContainerInterface
192
     */
193
    public function getDoctrineObjectManager()
194
    {
195
        return $this->doctrineObjectManager;
196
    }
197
198
    /**
199
     * Устанавливает плагин менеджер для получения ObjectManager'ов Doctrine2
200
     *
201
     * @param ContainerInterface $doctrineObjectManager
202
     *
203
     * @return $this
204
     */
205
    public function setDoctrineObjectManager(ContainerInterface $doctrineObjectManager)
206
    {
207
        $this->doctrineObjectManager = $doctrineObjectManager;
208
209
        return $this;
210
    }
211
}
212