ResolverByModuleContextMapFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 4
dl 4
loc 52
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createService() 4 30 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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