Completed
Push — master ( 54e9fc...26861c )
by Андрей
02:45
created

ObjectManagerAutoDetector::setDoctrineObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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