AbstractExecutorFactory::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
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\AbstractFactoryInterface;
9
use Zend\ServiceManager\AbstractPluginManager;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
use Nnx\DoctrineFixtureModule\Options\ModuleOptions;
12
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface;
13
14
15
/**
16
 * Class AbstractExecutorFactory
17
 *
18
 * @package Nnx\DoctrineFixtureModule\Executor
19
 */
20
class AbstractExecutorFactory implements AbstractFactoryInterface
21
{
22
    /**
23
     * Флаг определяет была ли инциализированна фабрика
24
     *
25
     * @var bool
26
     */
27
    protected $isInit = false;
28
29
    /**
30
     * Настройки модуля
31
     *
32
     * @var ModuleOptions
33
     */
34
    protected $moduleOptions;
35
36
    /**
37
     * Инициализация фабрики
38
     *
39
     * @param ServiceLocatorInterface $serviceLocator
40
     *
41
     * @return void
42
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
43
     */
44 View Code Duplication
    protected function init(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...
45
    {
46
        if ($this->isInit) {
47
            return null;
48
        }
49
50
        $appServiceLocator = $serviceLocator;
51
        if ($serviceLocator instanceof AbstractPluginManager) {
52
            $appServiceLocator = $serviceLocator->getServiceLocator();
53
        }
54
55
        /** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */
56
        $moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class);
57
        /** @var ModuleOptions $moduleOptions */
58
        $this->moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class);
59
60
        $this->isInit = true;
61
    }
62
63
64
    /**
65
     * @inheritDoc
66
     * 
67
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
68
     */
69
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
70
    {
71
        $this->init($serviceLocator);
72
73
        return array_key_exists($requestedName, $this->moduleOptions->getExecutors());
74
    }
75
76
    /**
77
     * @inheritDoc
78
     *
79
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
80
     */
81
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
82
    {
83
        $this->init($serviceLocator);
84
85
        $executors = $this->moduleOptions->getExecutors();
86
87
        $creationOptions = $executors[$requestedName];
88
89
        $creationOptions['name'] = $requestedName;
90
91
        /** @var FixtureExecutorManagerInterface  $serviceLocator*/
92
        return $serviceLocator->get(Executor::class, $creationOptions);
0 ignored issues
show
Unused Code introduced by
The call to ServiceLocatorInterface::get() has too many arguments starting with $creationOptions.

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...
93
    }
94
}
95