createServiceWithName()   C
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 59
Code Lines 37

Duplication

Lines 35
Ratio 59.32 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 35
loc 59
rs 6.5919
cc 10
eloc 37
nc 8
nop 3

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\Loader;
7
8
use Zend\ServiceManager\AbstractFactoryInterface;
9
use Zend\ServiceManager\AbstractPluginManager;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
12
use Nnx\DoctrineFixtureModule\Options\ModuleOptions;
13
use Nnx\DoctrineFixtureModule\Module;
14
use Doctrine\Fixture\Loader\ChainLoader;
15
16
17
/**
18
 * Class FixtureLoaderAbstractFactory
19
 *
20
 * @package Nnx\DoctrineFixtureModule\Loader
21
 */
22
class FixtureLoaderAbstractFactory
23
    implements AbstractFactoryInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
24
{
25
26
    /**
27
     * Флаг определеят была ли инициализирована фабрика
28
     *
29
     * @var bool
30
     */
31
    protected $isInit = false;
32
33
    /**
34
     * Конфиг с настройками загрузчиков фикстур
35
     *
36
     * @var array
37
     */
38
    protected $fixtureLoaderConfig = [];
39
40
    /**
41
     * Инициализация фабрики
42
     *
43
     * @param ServiceLocatorInterface $serviceLocator
44
     *
45
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
46
     */
47 View Code Duplication
    protected function initFactory(ServiceLocatorInterface $serviceLocator)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
49
        if (true === $this->isInit) {
50
            return;
51
        }
52
53
        $appServiceLocator = $serviceLocator;
54
        if ($serviceLocator instanceof AbstractPluginManager) {
55
            $appServiceLocator = $serviceLocator->getServiceLocator();
56
        }
57
58
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
59
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
60
        /** @var ModuleOptions $moduleOptions */
61
        $moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class);
62
63
        $this->fixtureLoaderConfig = $moduleOptions->getFixturesLoaders();
64
65
66
        $this->isInit = true;
67
    }
68
69
70
    /**
71
     * @inheritDoc
72
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
73
     */
74
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
75
    {
76
        $this->initFactory($serviceLocator);
77
78
        return array_key_exists($requestedName, $this->fixtureLoaderConfig);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
84
     * @throws \Nnx\DoctrineFixtureModule\Loader\Exception\RuntimeException
85
     */
86
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
87
    {
88
        $this->initFactory($serviceLocator);
89
90
        /** @var FixtureLoaderManagerInterface  $serviceLocator*/
91
92
93
        $loadersConfigs = $this->fixtureLoaderConfig[$requestedName];
94
95
        $chains = [];
96
        foreach ($loadersConfigs as $index => $item) {
97 View Code Duplication
            if (!is_array($item)) {
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...
98
                $errMsg = sprintf(
99
                    'Item [%s][fixtures][%s] of type %s is invalid. Must array',
100
                    Module::CONFIG_KEY,
101
                    $index,
102
                    (is_object($item) ? get_class($item) : gettype($item))
103
                );
104
                throw new Exception\RuntimeException($errMsg);
105
            }
106
107 View Code Duplication
            if (!array_key_exists('name', $item)) {
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...
108
                $errMsg = sprintf(
109
                    'Required parameter [%s][fixtures][%s][\'name\'] not found',
110
                    Module::CONFIG_KEY,
111
                    $index
112
                );
113
                throw new Exception\RuntimeException($errMsg);
114
            }
115
116 View Code Duplication
            if (!is_string($item['name'])) {
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...
117
                $errMsg = sprintf(
118
                    'Parameter [%s][fixtures][%s][\'name\'] of type %s is invalid. Must string',
119
                    Module::CONFIG_KEY,
120
                    $index,
121
                    (is_object($item['name']) ? get_class($item['name']) : gettype($item['name']))
122
                );
123
                throw new Exception\RuntimeException($errMsg);
124
            }
125
126
            $name = $item['name'];
127
            $options = array_key_exists('options', $item) ? $item['options'] : [];
128 View Code Duplication
            if (!is_array($options)) {
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...
129
                $errMsg = sprintf(
130
                    'Parameter [%s][fixtures][%s][\'options\'] of type %s is invalid. Must array',
131
                    Module::CONFIG_KEY,
132
                    $index,
133
                    (is_object($options) ? get_class($options) : gettype($options))
134
                );
135
                throw new Exception\RuntimeException($errMsg);
136
            }
137
138
            $chains[] = $serviceLocator->get($name, $options);
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with $options.

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...
139
        }
140
141
        return $serviceLocator->get(ChainLoader::class, [
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with array('loaderList' => $chains).

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...
142
            'loaderList' => $chains
143
        ]);
144
    }
145
}
146