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\Exception\InvalidArgumentException; |
17
|
|
|
use StateMachine\Payload; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Describes state machine event |
21
|
|
|
* |
22
|
|
|
* @package StateMachine |
23
|
|
|
*/ |
24
|
|
|
final class Event |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Event name |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Target state |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $targetState; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Error state |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $errorState; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Command executed when event is triggered |
49
|
|
|
* |
50
|
|
|
* @var callable |
51
|
|
|
*/ |
52
|
|
|
private $command; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Additional attributes |
56
|
|
|
* |
57
|
|
|
* @var Attributes |
58
|
|
|
*/ |
59
|
|
|
private $attributes; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor |
63
|
|
|
* |
64
|
|
|
* @param string $name event name |
65
|
|
|
* @param string $targetState state where subject will go when all commands were executed successfully |
66
|
|
|
* @param string $errorState state where subject will go when command execution failed |
67
|
|
|
* @param callable $command collection of commands |
68
|
|
|
* @param array $attributes additional attributes. |
69
|
|
|
* |
70
|
|
|
* @throws InvalidArgumentException |
71
|
|
|
*/ |
72
|
33 |
|
public function __construct( |
73
|
|
|
$name, |
74
|
|
|
$targetState = null, |
75
|
|
|
$errorState = null, |
76
|
|
|
callable $command = null, |
77
|
|
|
array $attributes = [] |
78
|
|
|
) { |
79
|
33 |
|
if (empty($name)) { |
80
|
1 |
|
throw InvalidArgumentException::emptyEventName(); |
81
|
|
|
} |
82
|
|
|
|
83
|
32 |
|
$this->name = $name; |
84
|
32 |
|
$this->targetState = $targetState; |
85
|
32 |
|
$this->errorState = $errorState; |
86
|
32 |
|
$this->command = $command; |
87
|
32 |
|
$this->attributes = new Attributes($attributes); |
88
|
32 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return event name |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
18 |
|
public function name(): string |
96
|
|
|
{ |
97
|
18 |
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return name of success state |
102
|
|
|
* Subject will go into this state when all commands execute properly |
103
|
|
|
* |
104
|
|
|
* @return null|string |
105
|
|
|
*/ |
106
|
10 |
|
public function targetState() |
107
|
|
|
{ |
108
|
10 |
|
return $this->targetState; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return error state name |
113
|
|
|
* State where subject will go when something goes wrong |
114
|
|
|
* |
115
|
|
|
* @return null|string |
116
|
|
|
*/ |
117
|
4 |
|
public function errorState() |
118
|
|
|
{ |
119
|
4 |
|
return $this->errorState; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return attributes container |
124
|
|
|
* |
125
|
|
|
* @return Attributes |
126
|
|
|
*/ |
127
|
1 |
|
public function attributes(): Attributes |
128
|
|
|
{ |
129
|
1 |
|
return $this->attributes; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Triggers event and return next state name or null if there is no state change |
134
|
|
|
* |
135
|
|
|
* @param Payload $payload |
136
|
|
|
* |
137
|
|
|
* @return null|string |
138
|
|
|
*/ |
139
|
12 |
|
public function trigger(Payload $payload) |
140
|
|
|
{ |
141
|
12 |
|
if (!$this->command) { |
142
|
5 |
|
return $this->targetState(); |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
return call_user_func($this->command, $payload) ? $this->targetState() : $this->errorState(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Return event string representation - its name |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
17 |
|
public function __toString(): string |
154
|
|
|
{ |
155
|
17 |
|
return $this->name(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|