Completed
Push — master ( 0a7e57...033978 )
by Thomas Mauro
02:31
created

AbstractServiceFactory::getFactoryMapping()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 8.8571
cc 3
eloc 17
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Facile\SentryModule\ServiceFactory;
4
5
use Interop\Container\ContainerInterface;
6
use Zend\ServiceManager\AbstractFactoryInterface;
7
use Zend\ServiceManager\Exception\ServiceNotFoundException;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
/**
11
 * Class AbstractServiceFactory
12
 *
13
 * @package Facile\SentryModule\ServiceFactory
14
 */
15
class AbstractServiceFactory 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
     * @var string
19
     */
20
    protected $configKey = 'facile';
21
22
    /**
23
     * @param ServiceLocatorInterface $services
24
     * @param string                  $name
25
     * @param string                  $requestedName
26
     * @return bool
27
     * @throws \Interop\Container\Exception\NotFoundException
28
     * @throws \Interop\Container\Exception\ContainerException
29
     */
30 4
    public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
31
    {
32 4
        return $this->canCreate($services, $requestedName);
33
    }
34
35
    /**
36
     * @param ContainerInterface $container
37
     * @param string             $requestedName
38
     * @return bool
39
     * @throws \Interop\Container\Exception\NotFoundException
40
     * @throws \Interop\Container\Exception\ContainerException
41
     */
42 4
    public function canCreate(ContainerInterface $container, $requestedName)
43
    {
44 4
        return false !== $this->getFactoryMapping($container, $requestedName);
45
    }
46
47
    /**
48
     * @param ContainerInterface $container
49
     * @param string             $name
50
     *
51
     * @return array
52
     * @throws \Interop\Container\Exception\NotFoundException
53
     * @throws \Interop\Container\Exception\ContainerException
54
     */
55 6
    private function getFactoryMapping(ContainerInterface $container, $name)
56
    {
57 6
        $matches = [];
58
59 6
        $pattern = '/^%s\.(?P<serviceType>[a-z0-9_]+)\.(?P<serviceName>[a-z0-9_]+)$/';
60 6
        $pattern = sprintf($pattern, $this->configKey . '.sentry');
61 6
        if (!preg_match($pattern, $name, $matches)) {
62 1
            return false;
63
        }
64
        /** @var array $config */
65 5
        $config = $container->get('config');
66 5
        $serviceType = $matches['serviceType'];
67 5
        $serviceName = $matches['serviceName'];
68
69 5
        $moduleConfig = $config[$this->configKey]['sentry'];
70 5
        $factoryConfig = $config[$this->configKey]['sentry_factories'];
71
72 5
        if (!isset($factoryConfig[$serviceType], $moduleConfig[$serviceType][$serviceName])) {
73 3
            return false;
74
        }
75
76
        return [
77 2
            'serviceType' => $serviceType,
78 2
            'serviceName' => $serviceName,
79 2
            'factoryClass' => $factoryConfig[$serviceType],
80 2
        ];
81
    }
82
83
    /**
84
     * @param ContainerInterface $container
85
     * @param string             $requestedName
86
     * @param array|null         $options
87
     * @return mixed
88
     * @throws \Interop\Container\Exception\NotFoundException
89
     * @throws \Interop\Container\Exception\ContainerException
90
     * @throws ServiceNotFoundException
91
     */
92 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
93
    {
94 2
        $mappings = $this->getFactoryMapping($container, $requestedName);
95
96 2
        if (!$mappings) {
97 1
            throw new ServiceNotFoundException();
98
        }
99
100 1
        $factoryClass = $mappings['factoryClass'];
101
        /* @var $factory \Facile\SentryModule\Service\AbstractFactory */
102 1
        $factory = new $factoryClass($mappings['serviceName']);
103
104 1
        return $factory($container, $requestedName);
105
    }
106
107
    /**
108
     * @param ServiceLocatorInterface $services
109
     * @param string  $name
110
     * @param string $requestedName
111
     * @return mixed
112
     */
113 2
    public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
114
    {
115 2
        return $this($services, $requestedName);
116
    }
117
}
118