AbstractPamiServiceFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 110
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 4 1
A __invoke() 0 14 2
A canCreateServiceWithName() 0 7 1
B getFactoryMapping() 0 23 4
A createServiceWithName() 0 7 1
1
<?php
2
3
namespace PamiModule\Factory;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use Zend\ServiceManager\AbstractFactoryInterface;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
use Zend\ServiceManager\Exception\ServiceNotFoundException;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
/**
13
 * Class AbstractPamiServiceFactory.
14
 */
15
class AbstractPamiServiceFactory 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...
16
{
17
    /**
18
     * Can the factory create an instance for the service?
19
     *
20
     * @param ContainerInterface $container
21
     * @param string             $requestedName
22
     *
23
     * @return bool
24
     */
25 2
    public function canCreate(ContainerInterface $container, $requestedName)
26
    {
27 2
        return false !== $this->getFactoryMapping($container, $requestedName);
28
    }
29
30
    /**
31
     * Create an object.
32
     *
33
     * @param ContainerInterface $container
34
     * @param string             $requestedName
35
     * @param null|array         $options
36
     *
37
     * @throws ServiceNotFoundException   if unable to resolve the service
38
     * @throws ServiceNotCreatedException if an exception is raised when
39
     *                                    creating a service
40
     * @throws ContainerException         if any other error occurs
41
     *
42
     * @return object
43
     */
44 3
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
45
    {
46 3
        $mappings = $this->getFactoryMapping($container, $requestedName);
47
48 3
        if (!$mappings) {
49 1
            throw new ServiceNotFoundException();
50
        }
51
52 2
        $factoryClass = $mappings['factoryClass'];
53
        /* @var $factory \PamiModule\Service\AbstractFactory */
54 2
        $factory = new $factoryClass($mappings['serviceName']);
55
56 2
        return $factory($container, \PamiModule\Service\AbstractFactory::class);
57
    }
58
59
    /**
60
     * Determine if we can create a service with name.
61
     *
62
     * @param ServiceLocatorInterface $serviceLocator Service Locator
63
     * @param string                  $name           Service name
64
     * @param string                  $requestedName  Service name requested
65
     *
66
     * @return bool
67
     */
68 1
    public function canCreateServiceWithName(
69
        ServiceLocatorInterface $serviceLocator,
70
        $name,
71
        $requestedName
72
    ) {
73 1
        return $this->canCreate($serviceLocator, $requestedName);
74
    }
75
76
    /**
77
     * Get the factoring map.
78
     *
79
     * @param ContainerInterface $container Service locator
80
     * @param string             $name      Service name
81
     *
82
     * @return bool|array
83
     */
84 4
    private function getFactoryMapping(ContainerInterface $container, $name)
85
    {
86 4
        $matches = [];
87
88 4
        if (!preg_match('/^pami\.(?P<serviceType>[a-z0-9_]+)\.(?P<serviceName>[a-z0-9_]+)$/', $name, $matches)) {
89 1
            return false;
90
        }
91
92 4
        $config = $container->get('config');
93 4
        $serviceType = $matches['serviceType'];
94 4
        $serviceName = $matches['serviceName'];
95
96 4
        if (!isset($config['pami_module_factories'][$serviceType]) ||
97 4
            !isset($config['pami_module'][$serviceType][$serviceName])) {
98 2
            return false;
99
        }
100
101
        return [
102 3
            'serviceType' => $serviceType,
103 3
            'serviceName' => $serviceName,
104 3
            'factoryClass' => $config['pami_module_factories'][$serviceType],
105 3
        ];
106
    }
107
108
    /**
109
     * Create service with name.
110
     *
111
     * @param ServiceLocatorInterface $serviceLocator Service locator
112
     * @param string                  $name           Service name
113
     * @param string                  $requestedName  Service name requested
114
     *
115
     * @return mixed
116
     */
117 2
    public function createServiceWithName(
118
        ServiceLocatorInterface $serviceLocator,
119
        $name,
120
        $requestedName
121
    ) {
122 2
        return $this($serviceLocator, $requestedName);
123
    }
124
}
125