Completed
Push — master ( 89c9f0...9aceec )
by Thomas Mauro
06:33
created

getServiceFactoryMapping()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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