getFactoryMapping()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.5625

Importance

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