Completed
Push — master ( 843e94...46dd70 )
by Андрей
6s
created

ResolverByModuleContextMapFactory::createService()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 30
Code Lines 16

Duplication

Lines 4
Ratio 13.33 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 4
loc 30
rs 8.439
cc 6
eloc 16
nc 32
nop 1
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 Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
9
use Zend\ServiceManager\AbstractPluginManager;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\MutableCreationOptionsInterface;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Zend\ServiceManager\MutableCreationOptionsTrait;
14
use ReflectionClass;
15
16
17
18
/**
19
 * Class ResolverByMapFactory
20
 *
21
 * @package Nnx\EntryNameResolver\EntryNameResolver
22
 */
23
class ResolverByModuleContextMapFactory implements FactoryInterface, MutableCreationOptionsInterface
24
{
25
    use MutableCreationOptionsTrait;
26
27
    /**
28
     * Имя создаваемого класса
29
     *
30
     * @var string
31
     */
32
    protected static $defaultTargetClassName = ResolverByModuleContextMap::class;
33
34
    /**
35
     * @inheritdoc
36
     *
37
     * @param ServiceLocatorInterface $serviceLocator
38
     *
39
     * @return ResolverByModuleContextMap
40
     * @throws \Nnx\EntryNameResolver\Exception\RuntimeException
41
     *
42
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
43
     */
44
    public function createService(ServiceLocatorInterface $serviceLocator)
45
    {
46
        $appServiceLocator = $serviceLocator;
47
        if ($serviceLocator instanceof AbstractPluginManager) {
48
            $appServiceLocator = $serviceLocator->getServiceLocator();
49
        }
50
51
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
52
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
53
54
55
        $creationOptions = $this->getCreationOptions();
56
        $options = is_array($creationOptions) ? $creationOptions : [];
57
58
        $contextMap = array_key_exists('contextMap', $options) ? $options['contextMap'] : [];
59
60
        $className = array_key_exists('className', $options) ? (string)$options['className'] : static::$defaultTargetClassName;
61
62
        $r = new ReflectionClass($className);
63
        $resolverByModuleContextMap = $r->newInstance($moduleOptionsPluginManager);
64
65 View Code Duplication
        if (!$resolverByModuleContextMap instanceof static::$defaultTargetClassName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $errMsg = sprintf('ResolverByModuleContextMap not implements: %s', static::$defaultTargetClassName);
67
            throw new Exception\RuntimeException($errMsg);
68
        }
69
70
        $resolverByModuleContextMap->setContextMap($contextMap);
71
72
        return $resolverByModuleContextMap;
73
    }
74
}
75