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

EntityManager::hasEntityClassByInterface()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\EntityManager;
7
8
use Zend\ServiceManager\AbstractPluginManager;
9
use Zend\ServiceManager\ConfigInterface;
10
11
12
/**
13
 * Class EntityManager
14
 *
15
 * @package Nnx\Doctrine\EntityManager
16
 */
17
class EntityManager extends AbstractPluginManager implements EntityManagerInterface
18
{
19
    /**
20
     * Имя секции в конфиге приложения отвечающей за настройки менеджера
21
     *
22
     * @var string
23
     */
24
    const CONFIG_KEY = 'nnx_entity_manager';
25
26
    /**
27
     * Кеш связывающий имя интерфейса и имя класса сущности
28
     *
29
     * @var array
30
     */
31
    protected $interfaceNameToEntityClass = [];
32
33
    /**
34
     * EntityManager constructor.
35
     *
36
     * @param ConfigInterface|null $configuration
37
     * @throws \Zend\ServiceManager\Exception\RuntimeException
38
     */
39
    public function __construct(ConfigInterface $configuration = null)
40
    {
41
        $this->setShareByDefault(false);
42
        parent::__construct($configuration);
43
    }
44
45
46
    /**
47
     * {@inheritDoc}
48
     *
49
     * @throws Exception\RuntimeException
50
     */
51
    public function validatePlugin($plugin)
52
    {
53
        if (is_object($plugin)) {
54
            return;
55
        }
56
57
        throw new Exception\RuntimeException(sprintf('Plugin of type %s is invalid', gettype($plugin)));
58
    }
59
60
    /**
61
     * @inheritdoc
62
     *
63
     * @param $interfaceName
64
     *
65
     * @return string
66
     *
67
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
68
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
69
     * @throws \Zend\ServiceManager\Exception\RuntimeException
70
     * @throws Exception\ErrorBuildEntityClassNameException
71
     */
72
    public function getEntityClassByInterface($interfaceName)
73
    {
74
        if (!$this->hasEntityClassByInterface($interfaceName)) {
75
            $errMsg = sprintf('Error build entity class name for %s', $interfaceName) ;
76
            throw new Exception\ErrorBuildEntityClassNameException($errMsg);
77
        }
78
79
        return $this->interfaceNameToEntityClass[$interfaceName];
80
    }
81
82
    /**
83
     * @inheritdoc
84
     *
85
     * @param $interfaceName
86
     *
87
     * @return boolean
88
     *
89
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
90
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
91
     * @throws \Zend\ServiceManager\Exception\RuntimeException
92
     */
93
    public function hasEntityClassByInterface($interfaceName)
94
    {
95
        if (array_key_exists($interfaceName, $this->interfaceNameToEntityClass)) {
96
            return false !== $this->interfaceNameToEntityClass[$interfaceName];
97
        }
98
99
        if (!$this->has($interfaceName)) {
100
            $this->interfaceNameToEntityClass[$interfaceName] = false;
101
            return false;
102
        }
103
104
        $entity = $this->get($interfaceName);
105
106
        if (!is_object($entity)) {
107
            $this->interfaceNameToEntityClass[$interfaceName] = false;
108
            return false;
109
        }
110
111
        $this->interfaceNameToEntityClass[$interfaceName] = get_class($entity);
112
113
        return true;
114
    }
115
}
116