|
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
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ObjectManagerAutoDetector |
|
12
|
|
|
* |
|
13
|
|
|
* @package Nnx\Doctrine\ObjectManager |
|
14
|
|
|
*/ |
|
15
|
|
|
class ObjectManagerAutoDetector implements ObjectManagerAutoDetectorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Менеджер для работы с настройками модуля |
|
19
|
|
|
* |
|
20
|
|
|
* @var ModuleOptionsPluginManagerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $moduleOptionsManager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ObjectManagerAutoDetector constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param ModuleOptionsPluginManagerInterface $moduleOptionsManager |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ModuleOptionsPluginManagerInterface $moduleOptionsManager) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setModuleOptionsManager($moduleOptionsManager); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Получает имя используемого в модуле ObjectManager'a Doctrine, по имени любого класса модуля |
|
36
|
|
|
* |
|
37
|
|
|
* @param $className |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
* |
|
41
|
|
|
* @throws Exception\RuntimeException |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function getObjectManagerNameByClassName($className) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$moduleOptions = $this->getModuleOptionsManager()->getOptionsByClassName($className); |
|
46
|
|
|
|
|
47
|
|
|
if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) { |
|
48
|
|
|
$errMsg = sprintf('Module options not implement %s', ObjectManagerNameProviderInterface::class); |
|
49
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$objectManagerName = $moduleOptions->getObjectManagerName(); |
|
53
|
|
|
|
|
54
|
|
|
if (!is_string($objectManagerName)) { |
|
55
|
|
|
$errMsg = 'Invalid object manager name. Manager name not string'; |
|
56
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $objectManagerName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Проверяет есть ли возможность по имени класса модуля, получить имя objectManager'a который используется в данном модуле |
|
65
|
|
|
* |
|
66
|
|
|
* @param $className |
|
67
|
|
|
* |
|
68
|
|
|
* @return boolean |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
public function hasObjectManagerNameByClassName($className) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$moduleOptions = $this->getModuleOptionsManager()->getOptionsByClassName($className); |
|
73
|
|
|
|
|
74
|
|
|
if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) { |
|
75
|
|
|
$errMsg = sprintf('Module options not implement %s', ObjectManagerNameProviderInterface::class); |
|
76
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$objectManagerName = $moduleOptions->getObjectManagerName(); |
|
80
|
|
|
|
|
81
|
|
|
if (!is_string($objectManagerName)) { |
|
82
|
|
|
$errMsg = 'Invalid object manager name. Manager name not string'; |
|
83
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $objectManagerName; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Возвращает менеджер для работы с настройками модуля |
|
95
|
|
|
* |
|
96
|
|
|
* @return ModuleOptionsPluginManagerInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getModuleOptionsManager() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->moduleOptionsManager; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Устанавливает менеджер для работы с настройками модуля |
|
105
|
|
|
* |
|
106
|
|
|
* @param ModuleOptionsPluginManagerInterface $moduleOptionsManager |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setModuleOptionsManager(ModuleOptionsPluginManagerInterface $moduleOptionsManager) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->moduleOptionsManager = $moduleOptionsManager; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.