GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractWorkflowFactory   B
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 200
Duplicated Lines 9 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 22
c 5
b 0
f 0
lcom 1
cbo 18
dl 18
loc 200
rs 7.3333

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 0 5 2
B createServiceWithName() 6 50 6
C buildWorkflowManagerConfig() 12 63 12
A getWorkflowConfigurationName() 0 4 1
A setWorkflowConfigurationName() 0 6 1

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/old-town/workflow-zf2
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace  OldTown\Workflow\ZF2\Factory;
7
8
use OldTown\Workflow\Util\Properties\Properties;
9
use OldTown\Workflow\Util\VariableResolverInterface;
10
use OldTown\Workflow\ZF2\Options\ConfigurationOptions;
11
use OldTown\Workflow\Config\ArrayConfiguration;
12
use Zend\ServiceManager\AbstractFactoryInterface;
13
use Zend\ServiceManager\MutableCreationOptionsInterface;
14
use Zend\ServiceManager\MutableCreationOptionsTrait;
15
use Zend\ServiceManager\ServiceLocatorInterface;
16
use OldTown\Workflow\ZF2\Options\ModuleOptions;
17
use OldTown\Workflow\WorkflowInterface;
18
use OldTown\Workflow\Config\ConfigurationInterface;
19
use OldTown\Workflow\Loader\WorkflowFactoryInterface;
20
use OldTown\Workflow\ZF2\ServiceEngine\Workflow;
21
use OldTown\Workflow\ZF2\Event\WorkflowManagerEvent;
22
23
24
/**
25
 * Class PluginMessageAbstractFactory
26
 *
27
 * @package OldTown\EventBus\Message
28
 */
29
class AbstractWorkflowFactory implements AbstractFactoryInterface, MutableCreationOptionsInterface
30
{
31
    use MutableCreationOptionsTrait;
32
33
    /**
34
     * Префикс с которого должно начинаться имя сервиса
35
     *
36
     * @var string
37
     */
38
    const SERVICE_NAME_PREFIX = 'workflow.manager.';
39
40
    /**
41
     * Имя опции через которое можно указать класс для создания конфиа для workflow
42
     *
43
     * @var string
44
     */
45
    const WORKFLOW_CONFIGURATION_NAME = 'workflowConfigurationName';
46
47
    /**
48
     * Имя класса конфигурации
49
     *
50
     * @var string
51
     */
52
    protected $workflowConfigurationName = ArrayConfiguration::class;
53
54
    /**
55
     * @param ServiceLocatorInterface $serviceLocator
56
     * @param                         $name
57
     * @param                         $requestedName
58
     *
59
     * @return bool
60
     */
61
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
62
    {
63
        $flag = 0 === strpos($requestedName, static::SERVICE_NAME_PREFIX) && strlen($requestedName) > strlen(static::SERVICE_NAME_PREFIX);
64
        return $flag;
65
    }
66
67
    /**
68
     * @param ServiceLocatorInterface $serviceLocator
69
     * @param                         $name
70
     * @param                         $requestedName
71
     *
72
     * @return WorkflowInterface
73
     *
74
     * @throws \OldTown\Workflow\ZF2\Factory\Exception\FactoryException
75
     */
76
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
77
    {
78
        $managerName = substr($requestedName, strlen(static::SERVICE_NAME_PREFIX));
79
80
        try {
81
            $creationOptions = $this->getCreationOptions();
82
            if (array_key_exists(static::WORKFLOW_CONFIGURATION_NAME, $creationOptions)) {
83
                $this->setWorkflowConfigurationName($creationOptions[static::WORKFLOW_CONFIGURATION_NAME]);
84
            }
85
86
            /** @var ModuleOptions $moduleOptions */
87
            $moduleOptions = $serviceLocator->get(ModuleOptions::class);
88
89
            $managerOptions = $moduleOptions->getManagerOptions($managerName);
90
            $configName = $managerOptions->getConfiguration();
91
92
            $workflowManagerName = $managerOptions->getName();
93
94
            $workflowManager = null;
95 View Code Duplication
            if ($serviceLocator->has($workflowManagerName)) {
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...
96
                $workflowManager = $serviceLocator->get($workflowManagerName);
97
            } elseif (class_exists($workflowManagerName)) {
98
                $r = new \ReflectionClass($workflowManagerName);
99
                $workflowManager = $r->newInstance($r);
100
            }
101
102
            if (!$workflowManager instanceof WorkflowInterface) {
103
                $errMsg = sprintf('Workflow not implements %s', WorkflowInterface::class);
104
                throw new Exception\InvalidWorkflowException($errMsg);
105
            }
106
107
            $configurationOptions = $moduleOptions->getConfigurationOptions($configName);
108
            $workflowConfig = $this->buildWorkflowManagerConfig($configurationOptions, $serviceLocator);
109
110
            $workflowManager->setConfiguration($workflowConfig);
111
112
            /** @var Workflow $workflowService */
113
            $workflowService = $serviceLocator->get(Workflow::class);
114
115
            $event = new WorkflowManagerEvent();
116
            $event->setWorkflowManager($workflowManager)
117
                  ->setName(WorkflowManagerEvent::EVENT_CREATE)
118
                  ->setTarget($workflowManager);
119
            $workflowService->getEventManager()->trigger($event);
120
        } catch (\Exception $e) {
121
            throw new Exception\FactoryException($e->getMessage(), $e->getCode(), $e);
122
        }
123
124
        return $workflowManager;
125
    }
126
127
128
    /**
129
     * Создает конфиг для workflow
130
     *
131
     * @param ConfigurationOptions    $config
132
     *
133
     * @param ServiceLocatorInterface $serviceLocator
134
     *
135
     * @return ConfigurationInterface
136
     *
137
     * @throws \OldTown\Workflow\ZF2\Factory\Exception\InvalidVariableResolverException
138
     * @throws \OldTown\Workflow\ZF2\Options\Exception\InvalidServiceConfigException
139
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
140
     * @throws \OldTown\Workflow\ZF2\Options\Exception\InvalidPersistenceConfigException
141
     * @throws \OldTown\Workflow\ZF2\Options\Exception\InvalidFactoryConfigException
142
     * @throws \OldTown\Workflow\ZF2\Factory\Exception\InvalidWorkflowFactoryException
143
     * @throws \OldTown\Workflow\ZF2\Factory\Exception\RuntimeException
144
     */
145
    public function buildWorkflowManagerConfig(ConfigurationOptions $config, ServiceLocatorInterface $serviceLocator)
146
    {
147
        $resolverServiceName = $config->getResolver();
148
        $resolver = null;
149
        if ($resolverServiceName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $resolverServiceName of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
150 View Code Duplication
            if ($serviceLocator->has($resolverServiceName)) {
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...
151
                $resolver = $serviceLocator->get($resolverServiceName);
0 ignored issues
show
Documentation introduced by
$resolverServiceName is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
            } elseif (class_exists($resolverServiceName)) {
153
                $r = new \ReflectionClass($resolverServiceName);
154
                $resolver = $r->newInstance();
155
            }
156
157
            if (!$resolver instanceof VariableResolverInterface) {
158
                $errMsg = sprintf('Resolver not implements %s', VariableResolverInterface::class);
159
                throw new Exception\InvalidVariableResolverException($errMsg);
160
            }
161
        }
162
163
        $factory = null;
164
        if ($config->hasFactoryOptions()) {
165
            $factoryServiceName = $config->getFactoryOptions()->getName();
166 View Code Duplication
            if ($serviceLocator->has($factoryServiceName)) {
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...
167
                $factory = $serviceLocator->get($factoryServiceName);
168
            } elseif (class_exists($resolverServiceName)) {
169
                $r = new \ReflectionClass($factoryServiceName);
170
                $factory = $r->newInstance();
171
            }
172
173
            if (!$factory instanceof WorkflowFactoryInterface) {
174
                $errMsg = sprintf('Factory not implements %s', WorkflowFactoryInterface::class);
175
                throw new Exception\InvalidWorkflowFactoryException($errMsg);
176
            }
177
            $factoryOptions = $config->getFactoryOptions()->getOptions();
178
            $properties = new Properties();
179
            foreach ($factoryOptions as $key => $value) {
180
                $properties->setProperty($key, $value);
181
            }
182
            $factory->init($properties);
183
        }
184
185
        $options = [
186
            ArrayConfiguration::PERSISTENCE => $config->getPersistenceOptions()->getName(),
187
            ArrayConfiguration::PERSISTENCE_ARGS => $config->getPersistenceOptions()->getOptions(),
188
            ArrayConfiguration::VARIABLE_RESOLVER => $resolver,
189
            ArrayConfiguration::WORKFLOW_FACTORY => $factory
190
191
        ];
192
193
        $configServiceName = $this->getWorkflowConfigurationName();
194
        if (!class_exists($configServiceName)) {
195
            $errMsg = sprintf('Class %s not found', $configServiceName);
196
            throw new Exception\RuntimeException($errMsg);
197
        }
198
        $r = new \ReflectionClass($configServiceName);
199
        $workflowManagerConfig = $r->newInstance($options);
200
201
        if (!$workflowManagerConfig instanceof ConfigurationInterface) {
202
            $errMsg = sprintf('Class not implement %s', ConfigurationInterface::class);
203
            throw new Exception\RuntimeException($errMsg);
204
        }
205
206
        return $workflowManagerConfig;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getWorkflowConfigurationName()
213
    {
214
        return $this->workflowConfigurationName;
215
    }
216
217
    /**
218
     * @param string $workflowConfigurationName
219
     *
220
     * @return $this
221
     */
222
    public function setWorkflowConfigurationName($workflowConfigurationName)
223
    {
224
        $this->workflowConfigurationName = (string)$workflowConfigurationName;
225
226
        return $this;
227
    }
228
}
229