ManagerRegistryFactory::createService()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/jms-serializer-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\JmsSerializerModule\Util;
7
8
use Zend\EventManager\EventManagerAwareTrait;
9
use Zend\ServiceManager\FactoryInterface;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
use Doctrine\Common\Persistence\ManagerRegistry;
12
13
/**
14
 * Class ManagerRegistryFactory
15
 *
16
 * @package Nnx\JmsSerializerModule\Util
17
 */
18
class ManagerRegistryFactory implements FactoryInterface
19
{
20
    use EventManagerAwareTrait;
21
22
    /**
23
     * Идендификатор EventManager'a
24
     *
25
     * @var array
26
     */
27
    protected $eventIdentifier = [
28
        'DoctrineManagerRegistry'
29
    ];
30
31
    /**
32
     * @param ServiceLocatorInterface $serviceLocator
33
     *
34
     * @return ManagerRegistry
35
     * @throws \Nnx\JmsSerializerModule\Util\Exception\RuntimeException
36
     */
37
    public function createService(ServiceLocatorInterface $serviceLocator)
38
    {
39
        //@see \Nnx\Doctrine\Listener\ManagerRegistryListener
40
        $results = $this->getEventManager()->trigger('get.doctrineManagerRegistry', $this, [], function ($managerRegistry) {
41
            return $managerRegistry instanceof ManagerRegistry;
42
        });
43
44
        $managerRegistry = $results->last();
45
46
        if (!$managerRegistry instanceof ManagerRegistry) {
47
            $errMsg = 'ManagerRegistry not found';
48
            throw new Exception\RuntimeException($errMsg);
49
        }
50
51
        return $managerRegistry;
52
    }
53
}
54