Completed
Push — dev ( 269014...5eca8b )
by Андрей
03:28
created

EntryNameResolverChainFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 57
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C createService() 0 39 8
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/entry-name-resolver
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\EntryNameResolver;
7
8
use Zend\ServiceManager\AbstractPluginManager;
9
use Zend\ServiceManager\FactoryInterface;
10
use Zend\ServiceManager\MutableCreationOptionsInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Zend\ServiceManager\MutableCreationOptionsTrait;
13
14
/**
15
 * Class EntryNameResolver
16
 *
17
 * @package Nnx\EntryNameResolver\EntryNameResolver
18
 */
19
class EntryNameResolverChainFactory implements FactoryInterface, MutableCreationOptionsInterface
20
{
21
    use MutableCreationOptionsTrait;
22
23
    /**
24
     * @inheritdoc
25
     *
26
     * @param ServiceLocatorInterface $serviceLocator
27
     *
28
     * @return EntryNameResolverChain
29
     *
30
     * @throws Exception\RuntimeException
31
     *
32
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
33
     * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException
34
     * @throws \Zend\ServiceManager\Exception\RuntimeException
35
     */
36
    public function createService(ServiceLocatorInterface $serviceLocator)
37
    {
38
        /** @var AbstractPluginManager $serviceLocator */
39
40
41
        $creationOptions = $this->getCreationOptions();
42
        $options = is_array($creationOptions) ? $creationOptions : [];
43
44
        $chain = new EntryNameResolverChain();
45
        foreach ($options as $entryNameResolverConfig) {
46
            if (!is_array($entryNameResolverConfig)) {
47
                $errMsg = 'Entry name resolver config is not array';
48
                throw new Exception\RuntimeException($errMsg);
49
            }
50
51
            if (!array_key_exists('name', $entryNameResolverConfig)) {
52
                $errMsg = 'Resolver entry name not found';
53
                throw new Exception\RuntimeException($errMsg);
54
            }
55
56
            $name = $entryNameResolverConfig['name'];
57
58
            $options = array_key_exists('options', $entryNameResolverConfig) ? $entryNameResolverConfig['options'] : [];
59
60
            if (!is_array($options)) {
61
                $errMsg = 'Resolver options is not array';
62
                throw new Exception\RuntimeException($errMsg);
63
            }
64
65
            /** @var EntryNameResolverInterface $resolver */
66
            $resolver = $serviceLocator->get($name, $options);
67
68
            $priority = array_key_exists('priority', $options) ? (integer)$options['priority'] : EntryNameResolverChain::DEFAULT_PRIORITY;
69
70
            $chain->attach($resolver, $priority);
71
        }
72
73
        return $chain;
74
    }
75
}
76