resolveConnectionByNameHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine\ManagerRegistry;
7
8
use Doctrine\Common\Persistence\ObjectManager;
9
use Zend\EventManager\AbstractListenerAggregate;
10
use Zend\EventManager\EventManagerInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Interop\Container\ContainerInterface;
13
14
/**
15
 * Class ResolverManagerRegistryResourceListener
16
 *
17
 * @package Nnx\Doctrine\ManagerRegistry
18
 */
19
class ResolverManagerRegistryResourceListener extends AbstractListenerAggregate
20
{
21
22
    /**
23
     * Локатор отвечающий за получение экземпляра ObjectManager'a Doctrine, по его имени
24
     *
25
     * @var ContainerInterface
26
     */
27
    protected $objectManagerLocator;
28
29
30
    /**
31
     * Локатор отвечающий за получение соеденения к базе данных, по его имени
32
     *
33
     * @var ServiceLocatorInterface
34
     */
35
    protected $connectionServiceLocator;
36
37
    /**
38
     * ResolverManagerRegistryResourceListener constructor.
39
     *
40
     * @param ContainerInterface $objectManagerLocator
41
     * @param ServiceLocatorInterface $connectionServiceLocator
42
     */
43
    public function __construct(ContainerInterface $objectManagerLocator, ServiceLocatorInterface $connectionServiceLocator)
44
    {
45
        $this->setObjectManagerLocator($objectManagerLocator);
46
        $this->setConnectionServiceLocator($connectionServiceLocator);
47
    }
48
49
    /**
50
     * @param EventManagerInterface $events
51
     *
52
     * @return mixed
53
     */
54
    public function attach(EventManagerInterface $events)
55
    {
56
        $sharedManager = $events->getSharedManager();
57
        $sharedManager->attach(ManagerRegistry::class, ManagerRegistryResourceEvent::RESOLVE_CONNECTION_EVENT, [$this, 'resolveConnectionByNameHandler']);
58
        $sharedManager->attach(ManagerRegistry::class, ManagerRegistryResourceEvent::RESOLVE_OBJECT_MANAGER_EVENT, [$this, 'resolveObjectManagerByNameHandler']);
59
    }
60
61
    /**
62
     * Обработчик события отвечающий за получение ObjectManager'a по его имени
63
     *
64
     * @param ManagerRegistryResourceEventInterface $e
65
     *
66
     * @return ObjectManager|null
67
     *
68
     * @throws \Interop\Container\Exception\ContainerException
69
     * @throws \Interop\Container\Exception\NotFoundException
70
     */
71
    public function resolveObjectManagerByNameHandler(ManagerRegistryResourceEventInterface $e)
72
    {
73
        $name = $e->getResourceName();
74
        $objectManagerLocator = $this->getObjectManagerLocator();
75
76
        $objectManager = null;
77
        if ($objectManagerLocator->has($name)) {
78
            $objectManager = $objectManagerLocator->get($name);
79
        }
80
81
        return $objectManager;
82
    }
83
84
85
    /**
86
     * Обработчик события отвечающего за получение соеденения к базе данных, по его имени
87
     *
88
     * @param ManagerRegistryResourceEventInterface $e
89
     *
90
     * @return mixed
91
     *
92
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
93
     */
94
    public function resolveConnectionByNameHandler(ManagerRegistryResourceEventInterface $e)
95
    {
96
        $name = $e->getResourceName();
97
        $connectionServiceLocator = $this->getConnectionServiceLocator();
98
99
        $connection = null;
100
        if ($connectionServiceLocator->has($name)) {
101
            $connection = $connectionServiceLocator->get($name);
102
        }
103
104
        return $connection;
105
    }
106
107
    /**
108
     * Возвращает  локатор отвечающий за получение экземпляра ObjectManager'a Doctrine, по его имени
109
     *
110
     * @return ContainerInterface
111
     */
112
    public function getObjectManagerLocator()
113
    {
114
        return $this->objectManagerLocator;
115
    }
116
117
    /**
118
     * Устанавливает локатор отвечающий за получение экземпляра ObjectManager'a Doctrine, по его имени
119
     *
120
     * @param ContainerInterface $objectManagerLocator
121
     *
122
     * @return $this
123
     */
124
    public function setObjectManagerLocator(ContainerInterface $objectManagerLocator)
125
    {
126
        $this->objectManagerLocator = $objectManagerLocator;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Возвращает локатор отвечающий за получение соеденения к базе данных, по его имени
133
     *
134
     * @return ServiceLocatorInterface
135
     */
136
    public function getConnectionServiceLocator()
137
    {
138
        return $this->connectionServiceLocator;
139
    }
140
141
    /**
142
     * Устанавливает локатор отвечающий за получение соеденения к базе данных, по его имени
143
     *
144
     * @param ServiceLocatorInterface $connectionServiceLocator
145
     *
146
     * @return $this
147
     */
148
    public function setConnectionServiceLocator(ServiceLocatorInterface $connectionServiceLocator)
149
    {
150
        $this->connectionServiceLocator = $connectionServiceLocator;
151
152
        return $this;
153
    }
154
}
155