Completed
Push — master ( 9307f6...6a1952 )
by Fabrice
05:58
created

FlowStatus::getException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 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
     * @var \Exception
36
     */
37
    protected $exception;
38
39
    /**
40
     * Flow statuses
41
     *
42
     * @var array
43
     */
44
    protected $flowStatuses = [
45
        self::FLOW_RUNNING   => self::FLOW_RUNNING,
46
        self::FLOW_CLEAN     => self::FLOW_CLEAN,
47
        self::FLOW_DIRTY     => self::FLOW_DIRTY,
48
        self::FLOW_EXCEPTION => self::FLOW_EXCEPTION,
49
    ];
50
51
    /**
52
     * Instantiate a Flow Status
53
     *
54
     * @param string          $status The flow status
55
     * @param \Exception|null $e
56
     *
57
     * @throws NodalFlowException
58
     */
59
    public function __construct($status, \Exception $e = null)
60
    {
61
        if (!isset($this->flowStatuses[$status])) {
62
            throw new NodalFlowException('$status must be one of :' . \implode(', ', $this->flowStatuses));
63
        }
64
65
        $this->status    = $status;
66
        $this->exception = $e;
67
    }
68
69
    /**
70
     * Get a string representation of the Flow status
71
     *
72
     * @return string The flow status
73
     */
74
    public function __toString()
75
    {
76
        return $this->getStatus();
77
    }
78
79
    /**
80
     * Indicate that the flow is currently running
81
     * useful for branched flow to find out what is
82
     * their parent up to and distinguish between top
83
     * parent end and branch end
84
     *
85
     * @return bool True If the flow is currently running
86
     */
87
    public function isRunning()
88
    {
89
        return $this->status === static::FLOW_RUNNING;
90
    }
91
92
    /**
93
     * Tells if the Flow went smoothly
94
     *
95
     * @return bool True If everything went well during the flow
96
     */
97
    public function isClean()
98
    {
99
        return $this->status === static::FLOW_CLEAN;
100
    }
101
102
    /**
103
     * Indicate that the flow was interrupted by a Node
104
     * s
105
     *
106
     * @return bool True If the flow was interrupted without exception
107
     */
108
    public function isDirty()
109
    {
110
        return $this->status === static::FLOW_DIRTY;
111
    }
112
113
    /**
114
     * Indicate that an exception was raised during the Flow execution
115
     *
116
     * @return bool True If the flow was interrupted with exception
117
     */
118
    public function isException()
119
    {
120
        return $this->status === static::FLOW_EXCEPTION;
121
    }
122
123
    /**
124
     * Return the Flow status
125
126
     * @return string The flow status
127
     */
128
    public function getStatus()
129
    {
130
        return $this->status;
131
    }
132
133
    /**
134
     * Return the eventual exception throw during the flow execution
135
     *
136
     * @return \Exception|null
137
     */
138
    public function getException()
139
    {
140
        return $this->exception;
141
    }
142
}
143