Completed
Push — master ( 3efb43...6ac6c3 )
by Андрей
05:02
created

ManagerRegistryFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 8
dl 0
loc 65
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createService() 0 38 5
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 Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
use Zend\EventManager\EventManagerAwareTrait;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Zend\Stdlib\ArrayUtils;
13
use Nnx\Doctrine\Options\ModuleOptionsInterface;
14
15
16
/**
17
 * Class ManagerRegistry
18
 *
19
 * @package Nnx\Doctrine\ManagerRegistry
20
 */
21
class ManagerRegistryFactory implements FactoryInterface
22
{
23
    use EventManagerAwareTrait;
24
25
    /**
26
     * Имя события бросаемое когда необходимо получить список используемых соеденений
27
     *
28
     * @var string
29
     */
30
    const EVENT_BUILD_LIST_CONNECTIONS = 'managerRegistryFactory.buildListConnections';
31
32
    /**
33
     * Имя события бросаемое когда необходимо получить список используемых ObjectManager'ов
34
     *
35
     * @var string
36
     */
37
    const EVENT_BUILD_LIST_OBJECT_MANAGERS = 'managerRegistryFactory.buildListObjectManagers';
38
39
    /**
40
     * @inheritdoc
41
     *
42
     * @param ServiceLocatorInterface $serviceLocator
43
     *
44
     * @return mixed
45
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
46
     */
47
    public function createService(ServiceLocatorInterface $serviceLocator)
48
    {
49
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
50
        $moduleOptionsPluginManager = $serviceLocator->get(ModuleOptionsPluginManagerInterface::class);
51
        /** @var ModuleOptionsInterface $moduleOptions */
52
        $moduleOptions = $moduleOptionsPluginManager->get(ModuleOptionsInterface::class);
53
54
        $managerRegistryOptions = $moduleOptions->getManagerRegistry();
55
56
57
        $buildListConnectionsResult = $this->getEventManager()->trigger(static::EVENT_BUILD_LIST_CONNECTIONS, $this);
58
        $listConnectionsResult = [];
59
        foreach ($buildListConnectionsResult as $item) {
60
            if (is_array($item)) {
61
                $listConnectionsResult = ArrayUtils::merge($listConnectionsResult, $item);
62
            }
63
        }
64
        $listConnectionsResult = ArrayUtils::merge($listConnectionsResult, $managerRegistryOptions->getConnections());
65
66
67
        $buildListObjectManagersResult = $this->getEventManager()->trigger(static::EVENT_BUILD_LIST_OBJECT_MANAGERS, $this);
68
        $listObjectManagersResult = [];
69
        foreach ($buildListObjectManagersResult as $item) {
70
            if (is_array($item)) {
71
                $listObjectManagersResult = ArrayUtils::merge($listObjectManagersResult, $item);
72
            }
73
        }
74
        $listObjectManagersResult = ArrayUtils::merge($listObjectManagersResult, $managerRegistryOptions->getObjectManagers());
75
76
        return new ManagerRegistry(
77
            ManagerRegistry::NAME,
78
            $listConnectionsResult,
79
            $listObjectManagersResult,
80
            $managerRegistryOptions->getDefaultConnection(),
81
            $managerRegistryOptions->getDefaultManager(),
82
            $managerRegistryOptions->getProxyInterfaceName()
83
        );
84
    }
85
}
86