EntityManagerFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 10 2
A getServiceLocator() 0 10 2
A setServiceLocator() 0 6 1
A getDefaultServiceLocator() 0 8 2
A setDefaultServiceLocator() 0 4 1
1
<?php
2
/**
3
 * @link https://github.com/old-town/workflow-doctrine-zf2
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\Doctrine\ZF2;
7
8
use Doctrine\ORM\EntityManagerInterface;
9
use OldTown\Workflow\Spi\Doctrine\EntityManagerFactory\EntityManagerFactoryInterface;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
13
/**
14
 * Class EntityManagerFactory
15
 *
16
 * @package OldTown\Workflow\Doctrine\ZF2
17
 */
18
class EntityManagerFactory implements EntityManagerFactoryInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    const ENTITY_MANAGER_NAME = 'entityManagerName';
24
25
    /**
26
     * Сервис локатор по умолчанию
27
     *
28
     * @var ServiceLocatorInterface
29
     */
30
    protected static $defaultServiceLocator;
31
32
    /**
33
     * Сервис локатор
34
     *
35
     * @var ServiceLocatorInterface
36
     */
37
    protected $serviceLocator;
38
39
    /**
40
     * Создает менеджер сущностей доктрины
41
     *
42
     * @param array $options
43
     *
44
     * @return EntityManagerInterface
45
     *
46
     * @throws Exception\InvalidArgumentException
47
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
48
     * @throws Exception\RuntimeException
49
     */
50
    public function factory(array $options = [])
51
    {
52
        if (!array_key_exists(static::ENTITY_MANAGER_NAME, $options)) {
53
            $errMsg = sprintf('Option %s not found', static::ENTITY_MANAGER_NAME);
54
            throw new Exception\InvalidArgumentException($errMsg);
55
        }
56
        $emName = $options[static::ENTITY_MANAGER_NAME];
57
58
        return $this->getServiceLocator()->get($emName);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getServiceLocator()->get($emName); of type object|array adds the type array to the return on line 58 which is incompatible with the return type declared by the interface OldTown\Workflow\Spi\Doc...ctoryInterface::factory of type Doctrine\ORM\EntityManagerInterface.
Loading history...
59
    }
60
61
    /**
62
     * @return ServiceLocatorInterface
63
     *
64
     * @throws \OldTown\Workflow\Doctrine\ZF2\Exception\RuntimeException
65
     */
66
    public function getServiceLocator()
67
    {
68
        if ($this->serviceLocator) {
69
            return $this->serviceLocator;
70
        }
71
72
        $this->serviceLocator = static::getDefaultServiceLocator();
73
74
        return $this->serviceLocator;
75
    }
76
77
    /**
78
     * @param ServiceLocatorInterface $serviceLocator
79
     *
80
     * @return $this
81
     */
82
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
83
    {
84
        $this->serviceLocator = $serviceLocator;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return ServiceLocatorInterface
91
     *
92
     * @throws Exception\RuntimeException
93
     */
94
    public static function getDefaultServiceLocator()
95
    {
96
        if (!static::$defaultServiceLocator instanceof ServiceLocatorInterface) {
97
            $errMsg = 'Service locator not found';
98
            throw new Exception\RuntimeException($errMsg);
99
        }
100
        return static::$defaultServiceLocator;
101
    }
102
103
    /**
104
     * @param ServiceLocatorInterface $defaultServiceLocator
105
     */
106
    public static function setDefaultServiceLocator(ServiceLocatorInterface $defaultServiceLocator)
107
    {
108
        static::$defaultServiceLocator = $defaultServiceLocator;
109
    }
110
}
111