Completed
Pull Request — develop (#683)
by Tom
01:23
created

AbstractDoctrineServiceFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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