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 — develop ( d16b40...f12d2b )
by David
11:46
created

Transition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 5
1
<?php
2
3
/**
4
 * Workflow library.
5
 *
6
 * @package    workflow
7
 * @author     David Molineus <[email protected]>
8
 * @copyright  2014-2017 netzmacht David Molineus
9
 * @license    LGPL 3.0 https://github.com/netzmacht/workflow
10
 * @filesource
11
 */
12
13
declare(strict_types=1);
14
15
namespace Netzmacht\Workflow\Flow;
16
17
use Netzmacht\Workflow\Data\ErrorCollection;
18
use Netzmacht\Workflow\Security\Permission;
19
use Netzmacht\Workflow\Base;
20
use Netzmacht\Workflow\Flow\Condition\Transition\AndCondition;
21
use Netzmacht\Workflow\Flow\Condition\Transition\Condition;
22
use Netzmacht\Workflow\Flow\Exception\ActionFailedException;
23
24
/**
25
 * Class Transition handles the transition from a step to another.
26
 *
27
 * @package Netzmacht\Workflow\Flow
28
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
29
 */
30
class Transition extends Base
31
{
32
    /**
33
     * Actions which will be executed during the transition.
34
     *
35
     * @var Action[]
36
     */
37
    private $actions = array();
38
39
    /**
40
     * Post actions which will be executed when new step is reached.
41
     *
42
     * @var Action[]
43
     */
44
    private $postActions = array();
45
46
    /**
47
     * The step the transition is moving to.
48
     *
49
     * @var Step
50
     */
51
    private $stepTo;
52
53
    /**
54
     * A pre condition which has to be passed to execute transition.
55
     *
56
     * @var AndCondition
57
     */
58
    private $preCondition;
59
60
    /**
61
     * A condition which has to be passed to execute the transition.
62
     *
63
     * @var AndCondition
64
     */
65
    private $condition;
66
67
    /**
68
     * A set of permission being assigned to the transition.
69
     *
70
     * @var Permission|null
71
     */
72
    private $permission;
73
74
    /**
75
     * The corresponding workflow.
76
     *
77
     * @var Workflow
78
     */
79
    private $workflow;
80
81
    /**
82
     * Transition constructor.
83
     *
84
     * @param string   $name     Name of the element.
85
     * @param Workflow $workflow The workflow to which the transition belongs.
86
     * @param Step     $stepTo   The target step.
87
     * @param string   $label    Label of the element.
88
     * @param array    $config   Configuration values.
89
     */
90
    public function __construct($name, Workflow $workflow, Step $stepTo, $label = null, array $config = [])
91
    {
92
        parent::__construct($name, $label, $config);
93
94
        $workflow->addTransition($this);
95
96
        $this->workflow = $workflow;
97
        $this->stepTo   = $stepTo;
98
    }
99
100
    /**
101
     * Get the workflow.
102
     *
103
     * @return Workflow
104
     */
105
    public function getWorkflow(): Workflow
106
    {
107
        return $this->workflow;
108
    }
109
110
    /**
111
     * Add an action to the transition.
112
     *
113
     * @param Action $action The added action.
114
     *
115
     * @return $this
116
     */
117
    public function addAction(Action $action): self
118
    {
119
        $this->actions[] = $action;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Get all actions.
126
     *
127
     * @return Action[]|iterable
128
     */
129
    public function getActions(): iterable
130
    {
131
        return $this->actions;
132
    }
133
134
    /**
135
     * Add an post action to the transition.
136
     *
137
     * @param Action $action The added action.
138
     *
139
     * @return $this
140
     */
141
    public function addPostAction(Action $action): self
142
    {
143
        $this->postActions[] = $action;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Get all post actions.
150
     *
151
     * @return Action[]|iterable
152
     */
153
    public function getPostActions(): iterable
154
    {
155
        return $this->postActions;
156
    }
157
158
    /**
159
     * Get the target step.
160
     *
161
     * @return Step
162
     */
163
    public function getStepTo():? Step
164
    {
165
        return $this->stepTo;
166
    }
167
168
    /**
169
     * Get the condition.
170
     *
171
     * @return Condition|null
172
     */
173
    public function getCondition():? Condition
174
    {
175
        return $this->condition;
176
    }
177
178
    /**
179
     * Add a condition.
180
     *
181
     * @param Condition $condition The new condition.
182
     *
183
     * @return $this
184
     */
185
    public function addCondition(Condition $condition)
186
    {
187
        if (!$this->condition) {
188
            $this->condition = new AndCondition();
189
        }
190
        $this->condition->addCondition($condition);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Get the precondition.
197
     *
198
     * @return Condition
199
     */
200
    public function getPreCondition():? Condition
201
    {
202
        return $this->preCondition;
203
    }
204
205
    /**
206
     * Add a precondition precondition.
207
     *
208
     * @param Condition $preCondition The new precondition.
209
     *
210
     * @return $this
211
     */
212
    public function addPreCondition(Condition $preCondition): self
213
    {
214
        if (!$this->preCondition) {
215
            $this->preCondition = new AndCondition();
216
        }
217
218
        $this->preCondition->addCondition($preCondition);
219
220
        return $this;
221
    }
222
223
    /**
224
     * Consider if user input is required.
225
     *
226
     * @param Item $item Workflow item.
227
     *
228
     * @return array
229
     */
230
    public function getRequiredPayloadProperties(Item $item): array
231
    {
232
        return array_merge(
233
            ... array_map(
234
                function (Action $action) use ($item) {
235
                    return $action->getRequiredPayloadProperties($item);
236
                },
237
                $this->actions
238
            )
239
        );
240
    }
241
242
    /**
243
     * Consider if transition is allowed.
244
     *
245
     * @param Item            $item            The Item.
246
     * @param Context         $context         The transition context.
247
     * @param ErrorCollection $errorCollection The error collection.
248
     *
249
     * @return bool
250
     */
251
    public function isAllowed(Item $item, Context $context, ErrorCollection $errorCollection): bool
252
    {
253
        if ($this->checkPreCondition($item, $context, $errorCollection)) {
254
            return $this->checkCondition($item, $context, $errorCollection);
255
        }
256
257
        return false;
258
    }
259
260
    /**
261
     * Consider if transition is available.
262
     *
263
     * If a transition can be available but it is not allowed depending on the user input.
264
     *
265
     * @param Item            $item            The Item.
266
     * @param Context         $context         The transition context.
267
     * @param ErrorCollection $errorCollection The error collection.
268
     *
269
     * @return bool
270
     */
271
    public function isAvailable(Item $item, Context $context, ErrorCollection $errorCollection): bool
272
    {
273
        if ($this->getRequiredPayloadProperties($item)) {
274
            return $this->checkPreCondition($item, $context, $errorCollection);
275
        }
276
277
        return $this->isAllowed($item, $context, $errorCollection);
278
    }
279
280
    /**
281
     * Check the precondition.
282
     *
283
     * @param Item            $item            The Item.
284
     * @param Context         $context         The transition context.
285
     * @param ErrorCollection $errorCollection The error collection.
286
     *
287
     * @return bool
288
     */
289
    public function checkPreCondition(Item $item, Context $context, ErrorCollection $errorCollection)
290
    {
291
        return $this->performConditionCheck($this->preCondition, $item, $context, $errorCollection);
292
    }
293
294
    /**
295
     * Check the condition.
296
     *
297
     * @param Item            $item            The Item.
298
     * @param Context         $context         The transition context.
299
     * @param ErrorCollection $errorCollection The error collection.
300
     *
301
     * @return bool
302
     */
303
    public function checkCondition(Item $item, Context $context, ErrorCollection $errorCollection)
304
    {
305
        return $this->performConditionCheck($this->condition, $item, $context, $errorCollection);
306
    }
307
308
    /**
309
     * Set a permission to the transition.
310
     *
311
     * @param Permission $permission Permission being assigned.
312
     *
313
     * @return $this
314
     */
315
    public function setPermission(Permission $permission)
316
    {
317
        $this->permission = $permission;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Consider if permission is assigned to transition.
324
     *
325
     * @param Permission $permission Permission being check.
326
     *
327
     * @return bool
328
     */
329
    public function hasPermission(Permission $permission)
330
    {
331
        if ($this->permission) {
332
            return $this->permission->equals($permission);
333
        }
334
335
        return false;
336
    }
337
338
    /**
339
     * Get assigned permission. Returns null if no transition is set.
340
     *
341
     * @return Permission|null
342
     */
343
    public function getPermission()
344
    {
345
        return $this->permission;
346
    }
347
348
    /**
349
     * Execute all actions.
350
     *
351
     * @param Item            $item            The workflow item.
352
     * @param Context         $context         The transition context.
353
     * @param ErrorCollection $errorCollection The error collection.
354
     *
355
     * @return bool
356
     */
357
    public function executeActions(Item $item, Context $context, ErrorCollection $errorCollection)
358
    {
359
        return $this->doExecuteActions($item, $context, $errorCollection, $this->actions);
360
    }
361
362
    /**
363
     * Execute all actions.
364
     *
365
     * @param Item            $item            The workflow item.
366
     * @param Context         $context         The transition context.
367
     * @param ErrorCollection $errorCollection The error collection.
368
     *
369
     * @return bool
370
     */
371
    public function executePostActions(Item $item, Context $context, ErrorCollection $errorCollection)
372
    {
373
        return $this->doExecuteActions($item, $context, $errorCollection, $this->postActions);
374
    }
375
376
    /**
377
     * Perform condition check.
378
     *
379
     * @param Condition|null  $condition       Condition to be checked.
380
     * @param Item            $item            Workflow item.
381
     * @param Context         $context         Condition context.
382
     * @param ErrorCollection $errorCollection Error collection.
383
     *
384
     * @return bool
385
     */
386
    private function performConditionCheck($condition, $item, $context, ErrorCollection $errorCollection)
387
    {
388
        if (!$condition) {
389
            return true;
390
        }
391
392
        return $condition->match($this, $item, $context, $errorCollection);
393
    }
394
395
    /**
396
     * Execute the actions.
397
     *
398
     * @param Item            $item            Workflow item.
399
     * @param Context         $context         Condition context.
400
     * @param ErrorCollection $errorCollection Error collection.
401
     * @param Action[]        $actions         Action to execute.
402
     *
403
     * @return bool
404
     */
405
    private function doExecuteActions(Item $item, Context $context, ErrorCollection $errorCollection, $actions)
406
    {
407
        $success = $this->isAllowed($item, $context, $errorCollection);
408
409
        if ($success) {
410
            try {
411
                foreach ($actions as $action) {
412
                    $action->transit($this, $item, $context);
413
                }
414
            } catch (ActionFailedException $e) {
415
                $params = array('exception' => $e->getMessage());
416
                $errorCollection->addError('transition.action.failed', $params);
417
418
                return false;
419
            }
420
        }
421
422
        return $success;
423
    }
424
}
425