ExecutorFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 22.68 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 0
cbo 9
dl 22
loc 97
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F createService() 22 86 11

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/doctrine-fixture-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\DoctrineFixtureModule\Executor;
7
8
use Nnx\DoctrineFixtureModule\FixtureInitializer\FixtureInitializerManagerInterface;
9
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
10
use Zend\ServiceManager\AbstractPluginManager;
11
use Zend\ServiceManager\FactoryInterface;
12
use Zend\ServiceManager\MutableCreationOptionsInterface;
13
use Zend\ServiceManager\MutableCreationOptionsTrait;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
use Nnx\DoctrineFixtureModule\Filter\FixtureFilterManagerInterface;
16
use Nnx\DoctrineFixtureModule\Loader\FixtureLoaderManagerInterface;
17
use ReflectionClass;
18
use Doctrine\Fixture\Configuration;
19
use Nnx\DoctrineFixtureModule\Options\ModuleOptions;
20
21
/**
22
 * Class ExecutorFactory
23
 *
24
 * @package Nnx\DoctrineFixtureModule\Executor
25
 */
26
class ExecutorFactory implements FactoryInterface, MutableCreationOptionsInterface
27
{
28
    use MutableCreationOptionsTrait;
29
30
    /**
31
     * @inheritDoc
32
     *
33
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
34
     * @throws \Nnx\DoctrineFixtureModule\Executor\Exception\RuntimeException
35
     */
36
    public function createService(ServiceLocatorInterface $serviceLocator)
37
    {
38
        $appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() :$serviceLocator;
39
40
        $creationOptions = $this->getCreationOptions();
41
42
        $configurationServiceName = DefaultExecutorConfiguration::class;
43
        if (array_key_exists('configuration', $creationOptions)) {
44
            $configurationServiceName = $creationOptions['configuration'];
45
        }
46
47 View Code Duplication
        if ($appServiceLocator->has($configurationServiceName)) {
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...
48
            /** @var Configuration $configuration */
49
            $configuration = $appServiceLocator->get($configurationServiceName);
50
        } elseif (class_exists($configurationServiceName)) {
51
            $r = new ReflectionClass($configurationServiceName);
52
            /** @var Configuration $configuration */
53
            $configuration = $r->newInstance();
54
        } else {
55
            $errMsg = 'Invalid fixture executor configuration';
56
            throw new Exception\RuntimeException($errMsg);
57
        }
58
59
        $builderServiceName = FixtureExecutorBuilderInterface::class;
60
        if (array_key_exists('builder', $creationOptions)) {
61
            $builderServiceName = $creationOptions['builder'];
62
        }
63 View Code Duplication
        if ($appServiceLocator->has($builderServiceName)) {
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...
64
            /** @var FixtureExecutorBuilderInterface $builder */
65
            $builder = $appServiceLocator->get($builderServiceName);
66
        } elseif (class_exists($builderServiceName)) {
67
            $r = new ReflectionClass($builderServiceName);
68
            /** @var FixtureExecutorBuilderInterface $builder */
69
            $builder = $r->newInstance();
70
        } else {
71
            $errMsg = 'Invalid fixture executor builder';
72
            throw new Exception\RuntimeException($errMsg);
73
        }
74
75
76
        /** @var FixtureInitializerManagerInterface $fixtureInitializerManager */
77
        $fixtureInitializerManager = $appServiceLocator->get(FixtureInitializerManagerInterface::class);
78
79
        $executor = new Executor($configuration, $builder, $fixtureInitializerManager);
80
81
82
        if (array_key_exists('fixturesLoader', $creationOptions)) {
83
            /** @var FixtureLoaderManagerInterface $fixtureLoaderManager */
84
            $fixtureLoaderManager = $appServiceLocator->get(FixtureLoaderManagerInterface::class);
85
86
            $loaderCreationOptions = [
87
                'contextExecutor' => $executor
88
            ];
89
            $fixtureLoader = $fixtureLoaderManager->get($creationOptions['fixturesLoader'], $loaderCreationOptions);
0 ignored issues
show
Unused Code introduced by
The call to FixtureLoaderManagerInterface::get() has too many arguments starting with $loaderCreationOptions.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
90
91
            $executor->setLoader($fixtureLoader);
92
        }
93
94
95
        if (array_key_exists('filter', $creationOptions)) {
96
            /** @var FixtureFilterManagerInterface $fixtureFilterManager */
97
            $fixtureFilterManager = $appServiceLocator->get(FixtureFilterManagerInterface::class);
98
99
            $filterCreationOptions = [
100
                'contextExecutor' => $executor
101
            ];
102
            $fixtureFilter = $fixtureFilterManager->get($creationOptions['filter'], $filterCreationOptions);
0 ignored issues
show
Unused Code introduced by
The call to FixtureFilterManagerInterface::get() has too many arguments starting with $filterCreationOptions.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
104
            $executor->setFilter($fixtureFilter);
105
        }
106
107
        if (array_key_exists('name', $creationOptions)) {
108
            $executor->setName($creationOptions['name']);
109
        }
110
111
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
112
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
113
114
        /** @var ModuleOptions $moduleOptions */
115
        $moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class);
116
117
        $executor->setContextInitializer($moduleOptions->getContextInitializer());
118
119
120
        return $executor;
121
    }
122
}
123