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