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.
Completed
Push — master ( 016559...2b724d )
by Андрей
03:44
created

RouteHandler::resolveEntryIdEventFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
 * @link https://github.com/old-town/workflow-zf2-dispatch
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace OldTown\Workflow\ZF2\Dispatch\RunParamsHandler;
7
8
use Zend\EventManager\AbstractListenerAggregate;
9
use Zend\EventManager\EventManagerAwareTrait;
10
use Zend\EventManager\EventManagerInterface;
11
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\Dispatcher;
12
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\WorkflowDispatchEventInterface;
13
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\RunWorkflowParam;
14
use OldTown\Workflow\ZF2\Dispatch\Metadata\ReaderInterface;
15
use Zend\Mvc\Controller\AbstractController;
16
use OldTown\Workflow\ZF2\Dispatch\Metadata\Target\RunParams\MetadataInterface;
17
use OldTown\Workflow\ZF2\Dispatch\RunParamsHandler\RouteHandler\ResolveEntryIdEvent;
18
use OldTown\Workflow\ZF2\Dispatch\RunParamsHandler\RouteHandler\ResolveEntryIdEventInterface;
19
use ReflectionClass;
20
21
22
/**
23
 * Class RouteHandler
24
 *
25
 * @package OldTown\Workflow\ZF2\Dispatch\RunParamsHandler
26
 */
27
class RouteHandler extends AbstractListenerAggregate
28
{
29
    use EventManagerAwareTrait;
30
31
    /**
32
     * Адаптер для чтения метаданных
33
     *
34
     * @var ReaderInterface
35
     */
36
    protected $metadataReader;
37
38
    /**
39
     * Имя класса событие, бросаемого когда требуется определить entryId
40
     *
41
     * @var string
42
     */
43
    protected $resolveEntryIdEventClassName = ResolveEntryIdEvent::class;
44
45
46
    /**
47
     * RouteHandler constructor.
48
     *
49
     * @param array $options
50
     */
51
    public function __construct(array $options = [])
52
    {
53
        $initOptions = [
54
            array_key_exists('metadataReader', $options) ? $options['metadataReader'] : null
55
        ];
56
        call_user_func_array([$this, 'init'], $initOptions);
57
    }
58
59
    /**
60
     * @param ReaderInterface $metadataReader
61
     */
62
    protected function init(ReaderInterface $metadataReader)
63
    {
64
        $this->setMetadataReader($metadataReader);
65
    }
66
67
    /**
68
     * @param EventManagerInterface $events
69
     */
70
    public function attach(EventManagerInterface $events)
71
    {
72
        $events->getSharedManager()->attach(Dispatcher::class, WorkflowDispatchEventInterface::METADATA_WORKFLOW_TO_RUN_EVENT, [$this, 'onMetadataWorkflowToRun'], 80);
73
    }
74
75
    /**
76
     * Получение метаданных для запуска workflow
77
     *
78
     * @param WorkflowDispatchEventInterface $e
79
     *
80
     * @return RunWorkflowParam|null
81
     *
82
     * @throws Exception\InvalidMetadataException
83
     * @throws Exception\ResolveEntryIdEventException
84
     */
85
    public function onMetadataWorkflowToRun(WorkflowDispatchEventInterface $e)
86
    {
87
        $mvcEvent = $e->getMvcEvent();
88
        $controller = $mvcEvent->getTarget();
89
        if (!$controller instanceof AbstractController) {
90
            return null;
91
        }
92
93
        $routeMatch = $mvcEvent->getRouteMatch();
94
        if (!$routeMatch) {
95
            return null;
96
        }
97
98
        $action = $routeMatch->getParam('action', 'not-found');
99
        $actionMethod = AbstractController::getMethodFromAction($action);
100
101
        if (!method_exists($controller, $actionMethod)) {
102
            return null;
103
        }
104
105
        $controllerClassName = get_class($controller);
106
107
        $metadata = $this->getMetadataReader()->loadMetadataForAction($controllerClassName, $actionMethod);
108
109 View Code Duplication
        if (!$metadata instanceof MetadataInterface) {
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...
110
            $errMsg = sprintf('Metadata not implement %s', MetadataInterface::class);
111
            throw new Exception\InvalidMetadataException($errMsg);
112
        }
113
114
        $workflowManagerNameParam = $metadata->getWorkflowManagerNameRouterParam();
115
        $workflowManagerName = $routeMatch->getParam($workflowManagerNameParam, null);
116
117
        $workflowActionNameParam = $metadata->getWorkflowActionNameRouterParam();
118
        $workflowActionName = $routeMatch->getParam($workflowActionNameParam, null);
119
120
        if (null === $workflowManagerName || null === $workflowActionName) {
121
            return null;
122
        }
123
124
        $runType = $e->getMetadata()->getWorkflowRunType();
125
126
        $workflowNameParam = $metadata->getWorkflowNameRouterParam();
127
        $workflowName = $routeMatch->getParam($workflowNameParam, null);
128
129
130
131
        $runWorkflowParam = new RunWorkflowParam();
132
        $runWorkflowParam->setRunType($runType);
133
        $runWorkflowParam->setManagerName($workflowManagerName);
134
        $runWorkflowParam->setActionName($workflowActionName);
135
136
        if (RunWorkflowParam::WORKFLOW_RUN_INITIALIZE === $runType && null !== $workflowName) {
137
            $runWorkflowParam->setWorkflowName($workflowName);
138
139
            return $runWorkflowParam;
140
        }
141
142
143
        if (RunWorkflowParam::WORKFLOW_RUN_TYPE_DO_ACTION === $runType) {
144
            $event = $this->resolveEntryIdEventFactory();
145
            $event->setWorkflowDispatchEvent($e);
146
            $event->setRunType($runType);
147
            $event->setActionName($workflowActionName);
148
            $event->setManagerName($workflowManagerName);
149
            $event->setWorkflowName($workflowName);
150
151
152
            $resolveEntryIdResults = $this->getEventManager()->trigger(ResolveEntryIdEventInterface::RESOLVE_ENTRY_ID_EVENT, $event, function ($item) {
153
                return is_numeric($item);
154
            });
155
            $resolveEntryIdResult = $resolveEntryIdResults->last();
156
157
            if (is_numeric($resolveEntryIdResult)) {
158
                $runWorkflowParam->setEntryId($resolveEntryIdResult);
159
160
                return $runWorkflowParam;
161
            }
162
        }
163
164
165
        return null;
166
    }
167
168
    /**
169
     * Подписчики по умолчанию
170
     *
171
     * @return void
172
     */
173
    public function attachDefaultListeners()
174
    {
175
        $this->getEventManager()->attach(ResolveEntryIdEventInterface::RESOLVE_ENTRY_ID_EVENT, [$this, 'onResolveEntryIdHandler']);
176
    }
177
178
    /**
179
     * Определение entryId на основе параметров роута
180
     *
181
     * @param ResolveEntryIdEventInterface $event
182
     *
183
     * @return integer|null
184
     *
185
     * @throws Exception\InvalidMetadataException
186
     */
187
    public function onResolveEntryIdHandler(ResolveEntryIdEventInterface $event)
188
    {
189
        $mvcEvent = $event->getWorkflowDispatchEvent()->getMvcEvent();
190
        $controller = $mvcEvent->getTarget();
191
        if (!$controller instanceof AbstractController) {
192
            return null;
193
        }
194
195
        $routeMatch = $mvcEvent->getRouteMatch();
196
        if (!$routeMatch) {
197
            return null;
198
        }
199
200
        $action = $routeMatch->getParam('action', 'not-found');
201
        $actionMethod = AbstractController::getMethodFromAction($action);
202
203
        if (!method_exists($controller, $actionMethod)) {
204
            return null;
205
        }
206
207
        $controllerClassName = get_class($controller);
208
        $metadata = $this->getMetadataReader()->loadMetadataForAction($controllerClassName, $actionMethod);
209
210 View Code Duplication
        if (!$metadata instanceof MetadataInterface) {
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...
211
            $errMsg = sprintf('Metadata not implement %s', MetadataInterface::class);
212
            throw new Exception\InvalidMetadataException($errMsg);
213
        }
214
215
        $entryIdParam = $metadata->getEntryIdRouterParam();
216
        return $routeMatch->getParam($entryIdParam, null);
217
    }
218
219
220
    /**
221
     * @return ReaderInterface
222
     */
223
    public function getMetadataReader()
224
    {
225
        return $this->metadataReader;
226
    }
227
228
    /**
229
     * @param ReaderInterface $metadataReader
230
     *
231
     * @return $this
232
     */
233
    public function setMetadataReader(ReaderInterface $metadataReader)
234
    {
235
        $this->metadataReader = $metadataReader;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Имя класса событие, бросаемого когда требуется определить entryId
242
     *
243
     * @return string
244
     */
245
    public function getResolveEntryIdEventClassName()
246
    {
247
        return $this->resolveEntryIdEventClassName;
248
    }
249
250
    /**
251
     * Устанавливает имя класса событие, бросаемого когда требуется определить entryId
252
     *
253
     * @param string $resolveEntryIdEventClassName
254
     *
255
     * @return $this
256
     */
257
    public function setResolveEntryIdEventClassName($resolveEntryIdEventClassName)
258
    {
259
        $this->resolveEntryIdEventClassName = $resolveEntryIdEventClassName;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Фабрика для создания объекта события бросаемого когда нужно определить значение entryId
266
     *
267
     * @return ResolveEntryIdEventInterface
268
     *
269
     * @throws Exception\ResolveEntryIdEventException
270
     */
271
    public function resolveEntryIdEventFactory()
272
    {
273
        $className = $this->getResolveEntryIdEventClassName();
274
275
        $r = new ReflectionClass($className);
276
        $event = $r->newInstance();
277
278
        if (!$event instanceof ResolveEntryIdEventInterface) {
279
            $errMsg = sprintf('ResolveEntryIdEvent not implement %s', ResolveEntryIdEventInterface::class);
280
            throw new Exception\ResolveEntryIdEventException($errMsg);
281
        }
282
283
        return $event;
284
    }
285
}
286