Completed
Push — master ( 8bbd25...4edf95 )
by Андрей
02:21
created

EntryNameResolverChainFactory::createService()   D

Complexity

Conditions 10
Paths 72

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 41
rs 4.8196
cc 10
eloc 21
nc 72
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $resolvers = array_key_exists('resolvers', $options) && is_array($options['resolvers']) ? $options['resolvers'] : [];
45
46
        $chain = new EntryNameResolverChain();
47
        foreach ($resolvers as $entryNameResolverConfig) {
48
            if (!is_array($entryNameResolverConfig)) {
49
                $errMsg = 'Entry name resolver config is not array';
50
                throw new Exception\RuntimeException($errMsg);
51
            }
52
53
            if (!array_key_exists('name', $entryNameResolverConfig)) {
54
                $errMsg = 'Resolver entry name not found';
55
                throw new Exception\RuntimeException($errMsg);
56
            }
57
58
            $name = $entryNameResolverConfig['name'];
59
60
            $resolverOptions = array_key_exists('options', $entryNameResolverConfig) ? $entryNameResolverConfig['options'] : [];
61
62
            if (!is_array($resolverOptions)) {
63
                $errMsg = 'Resolver options is not array';
64
                throw new Exception\RuntimeException($errMsg);
65
            }
66
67
            /** @var EntryNameResolverInterface $resolver */
68
            $resolver = $serviceLocator->get($name, $resolverOptions);
69
70
            $priority = array_key_exists('priority', $entryNameResolverConfig) ? (integer)$entryNameResolverConfig['priority'] : EntryNameResolverChain::DEFAULT_PRIORITY;
71
72
            $chain->attach($resolver, $priority);
73
        }
74
75
        return $chain;
76
    }
77
}
78