ExecutorFactory::createService()   F
last analyzed

Complexity

Conditions 11
Paths 276

Size

Total Lines 86
Code Lines 45

Duplication

Lines 22
Ratio 25.58 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 22
loc 86
rs 3.8181
cc 11
eloc 45
nc 276
nop 1

How to fix   Long Method    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/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