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 ( c6e2c9...016559 )
by Андрей
06:47 queued 02:23
created

AnnotationReader::setReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Metadata\Reader;
7
8
use OldTown\Workflow\ZF2\Dispatch\Metadata\Storage\DispatchConditionMetadata;
9
use OldTown\Workflow\ZF2\Dispatch\Metadata\Storage\MetadataInterface;
10
use Doctrine\Common\Annotations\AnnotationRegistry;
11
use Doctrine\Common\Annotations\Reader as DoctrineAnnotationsReaderInterface;
12
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
13
use OldTown\Workflow\ZF2\Dispatch\Metadata\Storage\Metadata;
14
use OldTown\Workflow\ZF2\Dispatch\Annotation as Annotation;
15
use OldTown\Workflow\ZF2\Dispatch\Annotation\Condition;
16
17
18
/**
19
 * Class AnnotationReader
20
 *
21
 * @package OldTown\Workflow\ZF2\Dispatch\Metadata\Reader
22
 */
23
class AnnotationReader implements ReaderInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    const READER_NAME = 'annotation';
29
30
    /**
31
     * @var DoctrineAnnotationsReaderInterface
32
     */
33
    protected $reader;
34
35
    /**
36
     * Кеш загруженных метаданных
37
     *
38
     * @var MetadataInterface[]
39
     */
40
    protected $classAnnotations = [];
41
42
    /**
43
     *
44
     * @throws Exception\AnnotationReaderException
45
     */
46
    public function __construct()
47
    {
48
        $this->init();
49
    }
50
51
    /**
52
     * Иницилазия
53
     *
54
     * @return void
55
     * @throws Exception\AnnotationReaderException
56
     */
57
    protected function init()
58
    {
59
        try {
60
            AnnotationRegistry::registerLoader(function ($class) {
61
                return (bool) class_exists($class);
62
            });
63
        } catch (\Exception $e) {
64
            throw new Exception\AnnotationReaderException($e->getMessage(), $e->getCode(), $e);
65
        }
66
    }
67
68
    /**
69
     * Получение метаданных для action контроллера
70
     *
71
     * @param string  $controllerClassName
72
     * @param  string $actionMethod
73
     *
74
     * @return MetadataInterface
75
     *
76
     * @throws \OldTown\Workflow\ZF2\Dispatch\Metadata\Exception\InvalidMetadataException
77
     * @throws Exception\AnnotationReaderException
78
     */
79
    public function loadMetadataForAction($controllerClassName, $actionMethod)
80
    {
81
        $key = $controllerClassName . '_' . $actionMethod;
82
        if (array_key_exists($key, $this->classAnnotations)) {
83
            return $this->classAnnotations[$key];
84
        }
85
86
        $r = new \ReflectionClass($controllerClassName);
87
        $rMethod = $r->getMethod($actionMethod);
88
89
        $metadata = new Metadata();
90
91
92
        /** @var Annotation\WorkflowDispatch|null $workflowDispatchAnnotation */
93
        $workflowDispatchAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\WorkflowDispatch::class);
94
        if (null !== $workflowDispatchAnnotation) {
95
            $metadata->setWorkflowDispatch($workflowDispatchAnnotation->enabled);
96
            $metadata->setWorkflowRunType($workflowDispatchAnnotation->activity);
97
        }
98
99
        /** @var Annotation\PrepareData|null $prepareDataAnnotation */
100
        $prepareDataAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\PrepareData::class);
101
        if ($prepareDataAnnotation) {
102
            $metadata->setFlagRunPrepareData($prepareDataAnnotation->enabled);
103
            $metadata->setPrepareDataMethod($prepareDataAnnotation->type);
104
            $metadata->setPrepareDataHandler($prepareDataAnnotation->handler);
105
        }
106
107
108
        /** @var Annotation\DispatchConditions|null $dispatchConditionsAnnotation */
109
        $dispatchConditionsAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\DispatchConditions::class);
110
        if ($dispatchConditionsAnnotation && is_array($dispatchConditionsAnnotation->conditions) && count($dispatchConditionsAnnotation->conditions) > 0) {
111
            $metadata->setFlagHasConditions(true);
112
            foreach ($dispatchConditionsAnnotation->conditions as $condition) {
113
                if (!$condition instanceof Condition) {
114
                    $errMsg = 'Invalid condition annotation';
115
                    throw new Exception\AnnotationReaderException($errMsg);
116
                }
117
                $conditionMetadata = new DispatchConditionMetadata($condition->type, $condition->handler, $condition->params);
118
119
                $metadata->addConditions($conditionMetadata);
120
            }
121
        }
122
123
        /** @var Annotation\WorkflowRouterMap|null $workflowRouterMapAnnotation */
124
        $workflowRouterMapAnnotation = $this->getReader()->getMethodAnnotation($rMethod, Annotation\WorkflowRouterMap::class);
125
        if ($workflowRouterMapAnnotation) {
126
            $metadata->setWorkflowManagerNameRouterParam($workflowRouterMapAnnotation->managerName);
127
            $metadata->setWorkflowActionNameRouterParam($workflowRouterMapAnnotation->actionName);
128
            $metadata->setWorkflowNameRouterParam($workflowRouterMapAnnotation->name);
129
            $metadata->setEntryIdRouterParam($workflowRouterMapAnnotation->entryId);
130
        }
131
132
        $metadata->validate();
133
134
        return $metadata;
135
    }
136
137
    /**
138
     * @return DoctrineAnnotationsReaderInterface
139
     */
140
    public function getReader()
141
    {
142
        if ($this->reader) {
143
            return $this->reader;
144
        }
145
146
        $this->reader = new DoctrineAnnotationReader();
147
148
        return $this->reader;
149
    }
150
151
    /**
152
     * @param DoctrineAnnotationsReaderInterface $reader
153
     *
154
     * @return $this
155
     */
156
    public function setReader(DoctrineAnnotationsReaderInterface $reader)
157
    {
158
        $this->reader = $reader;
159
160
        return $this;
161
    }
162
}
163