Completed
Push — master ( af7081...8b532e )
by Андрей
05:28 queued 03:12
created

ObjectManagerAutoDetector::hasObjectManagerNameByClassName()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.439
cc 5
eloc 17
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
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
     * Кеш, ключем являтся имя класса, а значением имя ObjectManager
26
     *
27
     * @var array
28
     */
29
    protected $classNameToObjectManagerName = [];
30
31
    /**
32
     * ObjectManagerAutoDetector constructor.
33
     *
34
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
35
     */
36
    public function __construct(ModuleOptionsPluginManagerInterface $moduleOptionsManager)
37
    {
38
        $this->setModuleOptionsManager($moduleOptionsManager);
39
    }
40
41
    /**
42
     * Получает имя используемого в модуле ObjectManager'a Doctrine, по имени любого класса модуля
43
     *
44
     * @param $className
45
     *
46
     * @return string
47
     *
48
     * @throws Exception\ErrorBuildObjectManagerNameException
49
     */
50
    public function getObjectManagerNameByClassName($className)
51
    {
52
        if (false === $this->hasObjectManagerNameByClassName($className)) {
53
            $errMsg = sprintf('Failed to get the manager\'s name for class %s', $className);
54
            throw new Exception\ErrorBuildObjectManagerNameException($errMsg);
55
        }
56
57
        return $this->classNameToObjectManagerName[$className];
58
    }
59
60
61
    /**
62
     * Проверяет есть ли возможность по имени класса модуля, получить имя objectManager'a который используется в данном модуле
63
     *
64
     * @param $className
65
     *
66
     * @return boolean
67
     *
68
     */
69
    public function hasObjectManagerNameByClassName($className)
70
    {
71
        if (array_key_exists($className, $this->classNameToObjectManagerName)) {
72
            return false !== $this->classNameToObjectManagerName[$className];
73
        }
74
75
        $moduleOptionsManager =  $this->getModuleOptionsManager();
76
        if (!$moduleOptionsManager->hasOptionsByClassName($className)) {
77
            $this->classNameToObjectManagerName[$className] = false;
78
            return false;
79
        }
80
81
        $moduleOptions = $moduleOptionsManager->getOptionsByClassName($className);
82
83
        if (!$moduleOptions instanceof ObjectManagerNameProviderInterface) {
84
            $this->classNameToObjectManagerName[$className] = false;
85
            return false;
86
        }
87
88
        $objectManagerName = $moduleOptions->getObjectManagerName();
89
90
        if (!is_string($objectManagerName)) {
91
            $this->classNameToObjectManagerName[$className] = false;
92
            return false;
93
        }
94
        $this->classNameToObjectManagerName[$className] = $objectManagerName;
95
96
        return false !== $this->classNameToObjectManagerName[$className];
97
    }
98
99
100
101
102
103
    /**
104
     * Возвращает менеджер для работы с настройками модуля
105
     *
106
     * @return ModuleOptionsPluginManagerInterface
107
     */
108
    public function getModuleOptionsManager()
109
    {
110
        return $this->moduleOptionsManager;
111
    }
112
113
    /**
114
     * Устанавливает менеджер для работы с настройками модуля
115
     *
116
     * @param ModuleOptionsPluginManagerInterface $moduleOptionsManager
117
     *
118
     * @return $this
119
     */
120
    public function setModuleOptionsManager(ModuleOptionsPluginManagerInterface $moduleOptionsManager)
121
    {
122
        $this->moduleOptionsManager = $moduleOptionsManager;
123
124
        return $this;
125
    }
126
}
127