Completed
Push — master ( f2b8b4...a21374 )
by Tom
24s queued 11s
created

AbstractDoctrineServiceFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0116
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 preg_match;
13
14
/**
15
 * Abstract service factory capable of instantiating services whose names match the
16
 * pattern <code>doctrine.$serviceType.$serviceName</doctrine>
17
 *
18
 * @link    http://www.doctrine-project.org/
19
 */
20
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...
21
{
22
    /**
23
     * {@inheritDoc}
24
     */
25 30
    public function canCreate(ContainerInterface $container, $requestedName)
26
    {
27 30
        return $this->getFactoryMapping($container, $requestedName) !== false;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 1
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
34
    {
35 1
        $mappings = $this->getFactoryMapping($container, $requestedName);
36
37 1
        if (! $mappings) {
38
            throw new ServiceNotFoundException();
39
        }
40
41 1
        $factoryClass = $mappings['factoryClass'];
42 1
        $factory      = new $factoryClass($mappings['serviceName']);
43
44 1
        return $factory->createService($container);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @deprecated
51
     */
52
    public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
53
    {
54
        return $this->canCreate($container, $requestedName);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     *
60
     * @deprecated
61
     */
62
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
63
    {
64
        return $this($serviceLocator, $requestedName);
65
    }
66
67
    /**
68
     * @return mixed[]|bool
69
     */
70 30
    private function getFactoryMapping(ContainerInterface $serviceLocator, string $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
        }
100
101
        if (! isset(
102
            $config['doctrine_factories'][$mappingType][$serviceType],
103
            $config['doctrine'][$mappingType][$serviceType][$serviceName]
104
        )) {
105
            return false;
106
        }
107
108
        return [
109
            'serviceType'  => $serviceType,
110
            'serviceName'  => $serviceName,
111
            'factoryClass' => $config['doctrine_factories'][$mappingType][$serviceType],
112
        ];
113
    }
114
}
115