Completed
Pull Request — master (#615)
by Filippo
14:02 queued 03:29
created

AbstractDoctrineServiceFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2.0116
1
<?php
2
3
namespace DoctrineModule\ServiceFactory;
4
5
use Interop\Container\ContainerInterface;
6
use Zend\ServiceManager\AbstractFactoryInterface;
7
use Zend\ServiceManager\Exception\ServiceNotFoundException;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
/**
11
 * Abstract service factory capable of instantiating services whose names match the
12
 * pattern <code>doctrine.$serviceType.$serviceName</doctrine>
13
 *
14
 * @license MIT
15
 * @link    http://www.doctrine-project.org/
16
 * @author  Marco Pivetta <[email protected]>
17
 */
18
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...
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 30
    public function canCreate(ContainerInterface $container, $requestedName)
24
    {
25 30
        return false !== $this->getFactoryMapping($container, $requestedName);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
32
    {
33 1
        $mappings = $this->getFactoryMapping($container, $requestedName);
34
35 1
        if (! $mappings) {
36
            throw new ServiceNotFoundException();
37
        }
38
39 1
        $factoryClass = $mappings['factoryClass'];
40
        /* @var $factory \DoctrineModule\Service\AbstractFactory */
41 1
        $factory = new $factoryClass($mappings['serviceName']);
42
43 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...
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @deprecated
49
     */
50
    public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
51
    {
52
        return $this->canCreate($container, $requestedName);
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     * @deprecated
58
     */
59
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
60
    {
61
        return $this($serviceLocator, $requestedName);
62
    }
63
64
    /**
65
     * @param ContainerInterface $serviceLocator
66
     * @param string             $name
67
     *
68
     * @return array|bool
69
     */
70 30
    private function getFactoryMapping(ContainerInterface $serviceLocator, $name)
71
    {
72 30
        $matches = [];
73
74 30
        if (! preg_match(
75 30
            '/^doctrine\.((?<mappingType>orm|odm)\.|)(?<serviceType>[a-z0-9_]+)\.(?<serviceName>[a-z0-9_]+)$/',
76 30
            $name,
77 30
            $matches
78
        )) {
79 11
            return false;
80
        }
81
82 20
        $config      = $serviceLocator->get('Config');
83 20
        $mappingType = $matches['mappingType'];
84 20
        $serviceType = $matches['serviceType'];
85 20
        $serviceName = $matches['serviceName'];
86
87 20
        if ($mappingType == '') {
88 20
            if (! isset($config['doctrine_factories'][$serviceType]) ||
89 20
                 ! isset($config['doctrine'][$serviceType][$serviceName])
90
            ) {
91 4
                return false;
92
            }
93
94
            return [
95 16
                'serviceType'  => $serviceType,
96 16
                'serviceName'  => $serviceName,
97 16
                'factoryClass' => $config['doctrine_factories'][$serviceType],
98
            ];
99
        } else {
100
            if (! isset($config['doctrine_factories'][$mappingType]) ||
101
                 ! isset($config['doctrine_factories'][$mappingType][$serviceType]) ||
102
                 ! isset($config['doctrine'][$mappingType][$serviceType][$serviceName])
103
            ) {
104
                return false;
105
            }
106
            return [
107
                'serviceType'  => $serviceType,
108
                'serviceName'  => $serviceName,
109
                'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType],
110
            ];
111
        }
112
    }
113
}
114