1 | <?php |
||
26 | final class Process implements ProcessInterface |
||
27 | { |
||
28 | /** |
||
29 | * Process/schema name |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $name; |
||
34 | |||
35 | /** |
||
36 | * Initial state |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $initialState; |
||
41 | |||
42 | /** |
||
43 | * Schema states |
||
44 | * |
||
45 | * @var States|State[] |
||
46 | */ |
||
47 | private $states = []; |
||
48 | |||
49 | /** |
||
50 | * @param string $name process/schema name |
||
51 | * @param string $initialState initial state for entities starting process |
||
52 | * @param State[] $states |
||
53 | * |
||
54 | * @throws InvalidArgumentException|InvalidStateException |
||
55 | */ |
||
56 | 11 | public function __construct($name, $initialState, array $states) |
|
57 | { |
||
58 | 11 | if (empty($name)) { |
|
59 | 1 | throw InvalidArgumentException::emptyProcessName(); |
|
60 | } |
||
61 | |||
62 | 10 | $this->name = $name; |
|
63 | 10 | $this->initialState = $initialState; |
|
64 | |||
65 | 10 | $this->states = new States($states); |
|
66 | |||
67 | 10 | if (!$this->states->has($this->initialState)) { |
|
68 | 1 | throw InvalidStateException::missingInitialState($this->name, $this->initialState); |
|
69 | } |
||
70 | 9 | } |
|
71 | |||
72 | /** |
||
73 | * Return process name |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 1 | public function name(): string |
|
78 | { |
||
79 | 1 | return $this->name; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Return initial state |
||
84 | * All entities without state will have this one |
||
85 | * |
||
86 | * @return State |
||
87 | */ |
||
88 | 5 | public function initialState(): State |
|
92 | |||
93 | /** |
||
94 | * Return state from collection by its name |
||
95 | * |
||
96 | * @param string $name |
||
97 | * |
||
98 | * @return State |
||
99 | */ |
||
100 | 6 | public function state($name): State |
|
104 | |||
105 | /** |
||
106 | * Return all states |
||
107 | * |
||
108 | * @return State[] |
||
109 | */ |
||
110 | 1 | public function states(): array |
|
114 | |||
115 | /** |
||
116 | * Trigger event for payload |
||
117 | * Return next state name |
||
118 | * |
||
119 | * @param string $event |
||
120 | * @param Payload $payload |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | public function triggerEvent($event, Payload $payload): string |
|
128 | } |
||
129 |