Passed
Push — master ( 0a7795...12d3c0 )
by Davis
01:51
created

AbstractWorkflow::isFinished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: davis
5
 * Date: 1/22/19
6
 * Time: 2:58 PM
7
 */
8
9
namespace Davispeixoto\Workflow;
10
11
use Davispeixoto\Workflow\Exceptions\InvalidTransitionException;
12
use Davispeixoto\Workflow\Interfaces\WorkflowInterface;
13
use MyCLabs\Enum\Enum;
14
15
abstract class AbstractWorkflow implements WorkflowInterface
16
{
17
    /**
18
     * @var Transition[]
19
     */
20
    protected $allowedTransitions;
21
22
    /**
23
     * @var Enum[]
24
     */
25
    protected $finishedStatus;
26
27
    /**
28
     * @var Enum
29
     */
30
    protected $currentStatus;
31
32
    /**
33
     * WorkflowInterface constructor.
34
     * @param Enum $initialStatus
35
     */
36 12
    public function __construct(Enum $initialStatus)
37
    {
38 12
        $this->currentStatus = $initialStatus;
39 12
    }
40
41
    /**
42
     * @return Enum
43
     */
44 7
    public function getCurrentStatus(): Enum
45
    {
46 7
        return $this->currentStatus;
47
    }
48
49
    /**
50
     * @return bool
51
     */
52 4
    public function isFinished(): bool
53
    {
54 4
        return in_array($this->currentStatus, $this->finishedStatus);
55
    }
56
57
    /**
58
     * @param Enum $status
59
     * @throws InvalidTransitionException
60
     */
61 12
    public function setCurrentStatus(Enum $status): void
62
    {
63 12
        $isTransitionAllowed = false;
64
65 12
        foreach ($this->allowedTransitions as $transition) {
66 12
            if ($transition->getFrom()->getValue() === $this->currentStatus->getValue()
67 12
                && $transition->getTo()->getValue() === $status->getValue()
68
            ) {
69 3
                $this->currentStatus = $status;
70 3
                $isTransitionAllowed = true;
71 12
                break;
72
            }
73
        }
74
75 12
        if (!$isTransitionAllowed) {
76 9
            throw new InvalidTransitionException('Transition not allowed');
77
        }
78 3
    }
79
}
80