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

ExecutorFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 25.58 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 7
dl 22
loc 86
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D createService() 22 75 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 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