AbstractServiceFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
3
namespace RabbitMqModule\Service;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
use Zend\ServiceManager\Exception\ServiceNotFoundException;
10
11
/**
12
 * Class AbstractServiceFactory
13
 * @package RabbitMqModule\Service
14
 */
15
class AbstractServiceFactory implements AbstractFactoryInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $configKey = 'rabbitmq_module';
21
22
    /**
23
     * Determine if we can create a service with name.
24
     *
25
     * @param ContainerInterface $serviceLocator
26
     * @param                         $requestedName
27
     *
28
     * @return bool
29
     */
30 4
    public function canCreate(ContainerInterface $serviceLocator, $requestedName)
31
    {
32 4
        return false !== $this->getFactoryMapping($serviceLocator, $requestedName);
33
    }
34
35
    /**
36
     * @param ContainerInterface $serviceLocator
37
     * @param string                                       $name
38
     *
39
     * @return bool|array
40
     */
41 6
    private function getFactoryMapping(ContainerInterface $serviceLocator, $name)
42
    {
43 6
        $matches = [];
44
45 6
        $pattern = sprintf('/^%s\.(?P<serviceType>[a-z0-9_]+)\.(?P<serviceName>[a-z0-9_]+)$/', $this->configKey);
46 6
        if (!preg_match($pattern, $name, $matches)) {
47 1
            return false;
48
        }
49
        /** @var array $config */
50 5
        $config = $serviceLocator->get('Configuration');
51 5
        $serviceType = $matches['serviceType'];
52 5
        $serviceName = $matches['serviceName'];
53
54 5
        $moduleConfig = $config[$this->configKey];
55 5
        $factoryConfig = $moduleConfig['factories'];
56
57 5
        if (!isset($factoryConfig[$serviceType], $moduleConfig[$serviceType][$serviceName])) {
58 4
            return false;
59
        }
60
61
        return [
62 2
            'serviceType' => $serviceType,
63 2
            'serviceName' => $serviceName,
64 2
            'factoryClass' => $factoryConfig[$serviceType],
65 2
        ];
66
    }
67
68
69
    /**
70
     * Create an object
71
     *
72
     * @param  ContainerInterface $container
73
     * @param  string $requestedName
74
     * @param  null|array $options
75
     * @return object
76
     * @throws ServiceNotFoundException if unable to resolve the service.
77
     * @throws ServiceNotCreatedException if an exception is raised when
78
     *     creating a service.
79
     * @throws ContainerException if any other error occurs
80
     */
81 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
82
    {
83 2
        $mappings = $this->getFactoryMapping($container, $requestedName);
84
85 2
        if (!$mappings) {
86 1
            throw new ServiceNotFoundException();
87
        }
88
89 1
        $factoryClass = $mappings['factoryClass'];
90
        /* @var $factory \RabbitMqModule\Service\AbstractFactory */
91 1
        $factory = new $factoryClass($mappings['serviceName']);
92
93 1
        return $factory($container, $requestedName, $options);
94
    }
95
}
96