Completed
Push — master ( c11d86...d758ed )
by Gianluca
13:50
created

AbstractDoctrineServiceFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 73.68%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 13
c 4
b 3
f 0
lcom 0
cbo 3
dl 0
loc 96
ccs 28
cts 38
cp 0.7368
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A __invoke() 0 14 2
A canCreateServiceWithName() 0 4 1
A createServiceWithName() 0 4 1
C getFactoryMapping() 0 43 8
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModule\ServiceFactory;
21
22
use Interop\Container\ContainerInterface;
23
use Zend\ServiceManager\AbstractFactoryInterface;
24
use Zend\ServiceManager\Exception\ServiceNotFoundException;
25
use Zend\ServiceManager\ServiceLocatorInterface;
26
27
/**
28
 * Abstract service factory capable of instantiating services whose names match the
29
 * pattern <code>doctrine.$serviceType.$serviceName</doctrine>
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Marco Pivetta <[email protected]>
34
 */
35
class AbstractDoctrineServiceFactory implements AbstractFactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\AbstractFactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\AbstractFactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
36
{
37
    /**
38
     * {@inheritDoc}
39
     */
40 30
    public function canCreate(ContainerInterface $container, $requestedName)
41
    {
42 30
        return false !== $this->getFactoryMapping($container, $requestedName);
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
49
    {
50 1
        $mappings = $this->getFactoryMapping($container, $requestedName);
51
52 1
        if (! $mappings) {
53
            throw new ServiceNotFoundException();
54
        }
55
56 1
        $factoryClass = $mappings['factoryClass'];
57
        /* @var $factory \DoctrineModule\Service\AbstractFactory */
58 1
        $factory = new $factoryClass($mappings['serviceName']);
59
60 1
        return $factory->createService($container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     * @deprecated
66
     */
67
    public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
68
    {
69
        return $this->canCreate($container, $requestedName);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     * @deprecated
75
     */
76
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
77
    {
78
        return $this($serviceLocator, $requestedName);
79
    }
80
81
    /**
82
     * @param ContainerInterface $serviceLocator
83
     * @param string             $name
84
     *
85
     * @return array|bool
86
     */
87 30
    private function getFactoryMapping(ContainerInterface $serviceLocator, $name)
88
    {
89 30
        $matches = array();
90
91 30
        if (!preg_match(
92 30
            '/^doctrine\.((?<mappingType>orm|odm)\.|)(?<serviceType>[a-z0-9_]+)\.(?<serviceName>[a-z0-9_]+)$/',
93 30
            $name,
94
            $matches
95 30
        )) {
96 10
            return false;
97
        }
98
99 20
        $config      = $serviceLocator->get('Config');
100 20
        $mappingType = $matches['mappingType'];
101 20
        $serviceType = $matches['serviceType'];
102 20
        $serviceName = $matches['serviceName'];
103
104 20
        if ($mappingType == '') {
105 20
            if (! isset($config['doctrine_factories'][$serviceType]) ||
106 18
                 ! isset($config['doctrine'][$serviceType][$serviceName])
107 20
            ) {
108 4
                return false;
109
            }
110
111
            return array(
112 16
                'serviceType'  => $serviceType,
113 16
                'serviceName'  => $serviceName,
114 16
                'factoryClass' => $config['doctrine_factories'][$serviceType],
115 16
            );
116
        } else {
117
            if (! isset($config['doctrine_factories'][$mappingType]) ||
118
                 ! isset($config['doctrine_factories'][$mappingType][$serviceType]) ||
119
                 ! isset($config['doctrine'][$mappingType][$serviceType][$serviceName])
120
            ) {
121
                return false;
122
            }
123
            return array(
124
                'serviceType'  => $serviceType,
125
                'serviceName'  => $serviceName,
126
                'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType],
127
            );
128
        }
129
    }
130
}
131