1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the StateMachine package |
7
|
|
|
* |
8
|
|
|
* (c) Michal Wachowski <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace StateMachine; |
15
|
|
|
|
16
|
|
|
use StateMachine\Payload; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* State machine |
20
|
|
|
* |
21
|
|
|
* @package StateMachine |
22
|
|
|
*/ |
23
|
|
|
class StateMachine |
24
|
|
|
{ |
25
|
|
|
const ON_STATE_WAS_SET = 'onStateWasSet'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Schema adapter |
29
|
|
|
* |
30
|
|
|
* @var ProcessInterface |
31
|
|
|
*/ |
32
|
|
|
private $process; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param Process $process |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct(Process $process) |
40
|
|
|
{ |
41
|
4 |
|
$this->process = $process; |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Trigger event for subject identified by identifier |
46
|
|
|
* Restore subject from handler by its identifier, then triggers event and saves subject |
47
|
|
|
* Return run history |
48
|
|
|
* |
49
|
|
|
* @param string $event |
50
|
|
|
* @param Payload $payload |
51
|
|
|
*/ |
52
|
4 |
|
public function triggerEvent($event, Payload $payload) |
53
|
|
|
{ |
54
|
4 |
|
if ($payload->state() === null) { |
55
|
4 |
|
$this->updatePayload($this->process->initialState(), $payload); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$state = $this->process->state($payload->state()); |
59
|
|
|
|
60
|
4 |
|
$nextStateName = $state->triggerEvent($event, $payload); |
61
|
4 |
|
if ($nextStateName === null) { |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
$state = $this->process->state($nextStateName); |
66
|
|
|
|
67
|
3 |
|
$this->updatePayload($state, $payload); |
68
|
3 |
|
$this->handleOnStateWasSet($state, $payload); |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Handles onStateWasSet event |
73
|
|
|
* Returns true if there was state change |
74
|
|
|
* |
75
|
|
|
* @param State $state |
76
|
|
|
* @param Payload $payload |
77
|
|
|
*/ |
78
|
3 |
|
private function handleOnStateWasSet(State $state, Payload $payload) |
79
|
|
|
{ |
80
|
3 |
|
while ($state->hasEvent(self::ON_STATE_WAS_SET)) { |
81
|
2 |
|
$newState = $state->triggerEvent(self::ON_STATE_WAS_SET, $payload); |
82
|
2 |
|
if ($newState === null) { |
83
|
1 |
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$state = $this->process->state($newState); |
87
|
1 |
|
$this->updatePayload($state, $payload); |
88
|
|
|
} |
89
|
3 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Update payload with new state data |
93
|
|
|
* |
94
|
|
|
* @param State $state |
95
|
|
|
* @param Payload $payload |
96
|
|
|
*/ |
97
|
4 |
|
private function updatePayload(State $state, Payload $payload) |
98
|
|
|
{ |
99
|
4 |
|
$payload->changeState($state->name()); |
100
|
4 |
|
} |
101
|
|
|
} |
102
|
|
|
|