Completed
Push — dev ( 64bfb0...f56b7a )
by Андрей
11:34
created

ExecutorFactory::createService()   D

Complexity

Conditions 11
Paths 276

Size

Total Lines 75
Code Lines 43

Duplication

Lines 22
Ratio 29.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 75
rs 4
cc 11
eloc 43
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 Zend\ServiceManager\AbstractPluginManager;
9
use Zend\ServiceManager\FactoryInterface;
10
use Zend\ServiceManager\MutableCreationOptionsInterface;
11
use Zend\ServiceManager\MutableCreationOptionsTrait;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
use Nnx\DoctrineFixtureModule\Filter\FixtureFilterManagerInterface;
14
use Nnx\DoctrineFixtureModule\Loader\FixtureLoaderManagerInterface;
15
use ReflectionClass;
16
use Doctrine\Fixture\Configuration;
17
18
/**
19
 * Class ExecutorFactory
20
 *
21
 * @package Nnx\DoctrineFixtureModule\Executor
22
 */
23
class ExecutorFactory implements FactoryInterface, MutableCreationOptionsInterface
24
{
25
    use MutableCreationOptionsTrait;
26
27
    /**
28
     * @inheritDoc
29
     *
30
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
31
     * @throws \Nnx\DoctrineFixtureModule\Executor\Exception\RuntimeException
32
     */
33
    public function createService(ServiceLocatorInterface $serviceLocator)
34
    {
35
        $appServiceLocator = $serviceLocator;
36
        if ($serviceLocator instanceof AbstractPluginManager) {
37
            $appServiceLocator = $serviceLocator->getServiceLocator();
38
        }
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
        $executor = new Executor($configuration, $builder);
75
76
77
        if (array_key_exists('fixturesLoader', $creationOptions)) {
78
            /** @var FixtureLoaderManagerInterface $fixtureLoaderManager */
79
            $fixtureLoaderManager = $appServiceLocator->get(FixtureLoaderManagerInterface::class);
80
81
            $loaderCreationOptions = [
82
                'contextExecutor' => $executor
83
            ];
84
            $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...
85
86
            $executor->setLoader($fixtureLoader);
87
        }
88
89
90
        if (array_key_exists('filter', $creationOptions)) {
91
            /** @var FixtureFilterManagerInterface $fixtureFilterManager */
92
            $fixtureFilterManager = $appServiceLocator->get(FixtureFilterManagerInterface::class);
93
94
            $filterCreationOptions = [
95
                'contextExecutor' => $executor
96
            ];
97
            $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...
98
99
            $executor->setFilter($fixtureFilter);
100
        }
101
102
        if (array_key_exists('name', $creationOptions)) {
103
            $executor->setName($creationOptions['name']);
104
        }
105
106
        return $executor;
107
    }
108
}
109