Passed
Push — master ( 5d141f...eaa040 )
by Fabrice
01:54
created

FlowStatus::isDirty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of NodalFlow.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\NodalFlow\Flows;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
14
/**
15
 * class FlowStatus
16
 */
17
class FlowStatus implements FlowStatusInterface
18
{
19
    /**
20
     * Flow statuses
21
     */
22
    const FLOW_RUNNING   = 'running';
23
    const FLOW_CLEAN     = 'clean';
24
    const FLOW_DIRTY     = 'dirty';
25
    const FLOW_EXCEPTION = 'exception';
26
27
    /**
28
     * Flow status
29
     *
30
     * @var string
31
     */
32
    protected $status;
33
34
    /**
35
     * Flow statuses
36
     *
37
     * @var array
38
     */
39
    protected $flowStatuses = [
40
        self::FLOW_RUNNING   => self::FLOW_RUNNING,
41
        self::FLOW_CLEAN     => self::FLOW_CLEAN,
42
        self::FLOW_DIRTY     => self::FLOW_DIRTY,
43
        self::FLOW_EXCEPTION => self::FLOW_EXCEPTION,
44
    ];
45
46
    /**
47
     * Instantiate a Flow Status
48
     *
49
     * @param string $status The flow status
50
     *
51
     * @throws NodalFlowException
52
     */
53
    public function __construct($status)
54
    {
55
        if (!isset($this->flowStatuses[$status])) {
56
            throw new NodalFlowException('$status must be one of :' . \implode(', ', $this->flowStatuses));
57
        }
58
59
        $this->status = $status;
60
    }
61
62
    /**
63
     * Get a string representation of the Flow status
64
     *
65
     * @return string The flow status
66
     */
67
    public function __toString()
68
    {
69
        return $this->getStatus();
70
    }
71
72
    /**
73
     * Indicate that the flow is currently running
74
     * usefull for branched flow to find out what is
75
     * their parent up to and distinguish between top
76
     * parent end and branch end
77
     *
78
     * @return bool True If the flow is currently running
79
     */
80
    public function isRunning()
81
    {
82
        return $this->status === static::FLOW_RUNNING;
83
    }
84
85
    /**
86
     * Tells if the Flow went smoothely
87
     *
88
     * @return bool True If everything went well during the flow
89
     */
90
    public function isClean()
91
    {
92
        return $this->status === static::FLOW_CLEAN;
93
    }
94
95
    /**
96
     * Indicate that the flow was interrupted by a Node
97
     * s
98
     *
99
     * @return bool True If the flow was interrupted without exception
100
     */
101
    public function isDirty()
102
    {
103
        return $this->status === static::FLOW_DIRTY;
104
    }
105
106
    /**
107
     * Indicate that an exception was raised during the Flow execution
108
     *
109
     * @return bool True If the flow was interrupted with exception
110
     */
111
    public function isException()
112
    {
113
        return $this->status === static::FLOW_EXCEPTION;
114
    }
115
116
    /**
117
     * Return the Flow status
118
119
     * @return string The flow status
120
     */
121
    public function getStatus()
122
    {
123
        return $this->status;
124
    }
125
}
126