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 — dev ( 40bda2...5b7120 )
by Андрей
04:01
created

RouteHandler::onMetadataWorkflowToRun()   C

Complexity

Conditions 13
Paths 6

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
rs 6.4268
cc 13
eloc 37
nc 6
nop 1

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/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\EventManagerInterface;
10
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\Dispatcher;
11
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\WorkflowDispatchEventInterface;
12
use OldTown\Workflow\ZF2\Dispatch\Dispatcher\RunWorkflowParam;
13
use OldTown\Workflow\ZF2\Dispatch\Metadata\ReaderInterface;
14
use Zend\Mvc\Controller\AbstractController;
15
use OldTown\Workflow\ZF2\Dispatch\Metadata\Target\RunParams\MetadataInterface;
16
17
/**
18
 * Class RouteHandler
19
 *
20
 * @package OldTown\Workflow\ZF2\Dispatch\RunParamsHandler
21
 */
22
class RouteHandler extends AbstractListenerAggregate
23
{
24
    /**
25
     * Адаптер для чтения метаданных
26
     *
27
     * @var ReaderInterface
28
     */
29
    protected $metadataReader;
30
31
    /**
32
     * RouteHandler constructor.
33
     *
34
     * @param array $options
35
     */
36
    public function __construct(array $options = [])
37
    {
38
        $initOptions = [
39
            array_key_exists('metadataReader', $options) ? $options['metadataReader'] : null
40
        ];
41
        call_user_func_array([$this, 'init'], $initOptions);
42
    }
43
44
    /**
45
     * @param ReaderInterface $metadataReader
46
     */
47
    protected function init(ReaderInterface $metadataReader)
48
    {
49
        $this->setMetadataReader($metadataReader);
50
    }
51
52
    /**
53
     * @param EventManagerInterface $events
54
     */
55
    public function attach(EventManagerInterface $events)
56
    {
57
        $events->getSharedManager()->attach(Dispatcher::class, WorkflowDispatchEventInterface::METADATA_WORKFLOW_TO_RUN_EVENT, [$this, 'onMetadataWorkflowToRun'], 80);
58
    }
59
60
    /**
61
     * Получение метаданных для запуска workflow
62
     *
63
     * @param WorkflowDispatchEventInterface $e
64
     *
65
     * @return RunWorkflowParam
66
     *
67
     * @throws Exception\InvalidMetadataException
68
     */
69
    public function onMetadataWorkflowToRun(WorkflowDispatchEventInterface $e)
70
    {
71
        $mvcEvent = $e->getMvcEvent();
72
        $controller = $mvcEvent->getTarget();
73
        if (!$controller instanceof AbstractController) {
74
            return null;
75
        }
76
77
        $routeMatch = $mvcEvent->getRouteMatch();
78
        if (!$routeMatch) {
79
            return null;
80
        }
81
82
        $action = $routeMatch->getParam('action', 'not-found');
83
        $actionMethod = AbstractController::getMethodFromAction($action);
84
85
        if (!method_exists($controller, $actionMethod)) {
86
            return null;
87
        }
88
89
        $controllerClassName = get_class($controller);
90
91
        $metadata = $this->getMetadataReader()->loadMetadataForAction($controllerClassName, $actionMethod);
92
93
        if (!$metadata instanceof MetadataInterface) {
94
            $errMsg = sprintf('Metadata not implement %s', MetadataInterface::class);
95
            throw new Exception\InvalidMetadataException($errMsg);
96
        }
97
98
        $workflowManagerNameParam = $metadata->getWorkflowManagerNameRouterParam();
99
        $workflowManagerName = $routeMatch->getParam($workflowManagerNameParam, null);
100
101
        $workflowActionNameParam = $metadata->getWorkflowActionNameRouterParam();
102
        $workflowActionName = $routeMatch->getParam($workflowActionNameParam, null);
103
104
        $workflowNameParam = $metadata->getWorkflowNameRouterParam();
105
        $workflowName = $routeMatch->getParam($workflowNameParam, null);
106
107
        $entryIdParam = $metadata->getEntryIdRouterParam();
108
        $entryId = $routeMatch->getParam($entryIdParam, null);
109
110
111
        $runType = $e->getMetadata()->getWorkflowRunType();
112
        if (null !== $workflowManagerName && null !== $workflowActionName
113
            && ((RunWorkflowParam::WORKFLOW_RUN_INITIALIZE === $runType && null !== $workflowName && null === $entryId)
114
                ||(RunWorkflowParam::WORKFLOW_RUN_TYPE_DO_ACTION === $runType && null !== $entryId && null === $workflowName))
115
        ) {
116
            $runWorkflowParam = new RunWorkflowParam();
117
            $runWorkflowParam->setRunType($runType);
118
            $runWorkflowParam->setManagerName($workflowManagerName);
119
            $runWorkflowParam->setActionName($workflowActionName);
120
            $runWorkflowParam->setEntryId($entryId);
121
            $runWorkflowParam->setWorkflowName($workflowName);
122
123
            return $runWorkflowParam;
124
        }
125
126
        return null;
127
    }
128
129
    /**
130
     * @return ReaderInterface
131
     */
132
    public function getMetadataReader()
133
    {
134
        return $this->metadataReader;
135
    }
136
137
    /**
138
     * @param ReaderInterface $metadataReader
139
     *
140
     * @return $this
141
     */
142
    public function setMetadataReader(ReaderInterface $metadataReader)
143
    {
144
        $this->metadataReader = $metadataReader;
145
146
        return $this;
147
    }
148
}
149