1 | <?php declare(strict_types=1); |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | /** |
||
0 ignored issues
–
show
|
|||
3 | * This file is part of the php-state project. |
||
4 | * |
||
5 | * (c) Yannick Voyer <[email protected]> (http://github.com/yvoyer) |
||
6 | */ |
||
0 ignored issues
–
show
|
|||
7 | |||
8 | namespace Star\Component\State; |
||
9 | |||
10 | use Star\Component\State\Callbacks\AlwaysThrowExceptionOnFailure; |
||
11 | use Star\Component\State\Callbacks\TransitionCallback; |
||
12 | use Star\Component\State\Event\StateEventStore; |
||
13 | use Star\Component\State\Event\TransitionWasFailed; |
||
14 | use Star\Component\State\Event\TransitionWasSuccessful; |
||
15 | use Star\Component\State\Event\TransitionWasRequested; |
||
16 | |||
17 | final class StateMachine |
||
0 ignored issues
–
show
|
|||
18 | { |
||
0 ignored issues
–
show
|
|||
19 | /** |
||
0 ignored issues
–
show
|
|||
20 | * @var EventRegistry |
||
21 | */ |
||
22 | private $listeners; |
||
0 ignored issues
–
show
|
|||
23 | |||
24 | /** |
||
0 ignored issues
–
show
|
|||
25 | * @var StateRegistry |
||
26 | */ |
||
27 | private $states; |
||
0 ignored issues
–
show
|
|||
28 | |||
29 | /** |
||
0 ignored issues
–
show
|
|||
30 | * @var string |
||
31 | */ |
||
32 | private $currentState; |
||
0 ignored issues
–
show
|
|||
33 | |||
34 | 46 | public function __construct( |
|
0 ignored issues
–
show
|
|||
35 | string $currentState, |
||
36 | StateRegistry $states, |
||
37 | EventRegistry $listeners |
||
38 | ) { |
||
0 ignored issues
–
show
|
|||
39 | 46 | $this->listeners = $listeners; |
|
40 | 46 | $this->states = $states; |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
41 | 46 | $this->setCurrentState($currentState); |
|
42 | 46 | } |
|
0 ignored issues
–
show
|
|||
43 | |||
44 | /** |
||
0 ignored issues
–
show
|
|||
45 | * @param string $transitionName The transition name |
||
0 ignored issues
–
show
|
|||
46 | * @param mixed $context |
||
0 ignored issues
–
show
|
|||
47 | * @param TransitionCallback|null $callback |
||
0 ignored issues
–
show
|
|||
48 | * |
||
49 | * @return string The next state to store on your context |
||
50 | * @throws InvalidStateTransitionException |
||
0 ignored issues
–
show
|
|||
51 | * @throws NotFoundException |
||
0 ignored issues
–
show
|
|||
52 | */ |
||
53 | 30 | public function transit(string $transitionName, $context, TransitionCallback $callback = null): string |
|
0 ignored issues
–
show
|
|||
54 | { |
||
0 ignored issues
–
show
|
|||
55 | 30 | if (! $callback) { |
|
56 | 29 | $callback = new AlwaysThrowExceptionOnFailure(); |
|
57 | } |
||
58 | |||
59 | 30 | $this->listeners->dispatch( |
|
60 | 30 | StateEventStore::BEFORE_TRANSITION, |
|
61 | 30 | new TransitionWasRequested($transitionName) |
|
62 | ); |
||
63 | |||
64 | 30 | $transition = $this->states->getTransition($transitionName); |
|
65 | 29 | $callback->beforeStateChange($context, $this); |
|
66 | |||
67 | 29 | $newState = $transition->getDestinationState(); |
|
68 | 29 | $allowed = $this->states->transitionStartsFrom($transitionName, $this->currentState); |
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
69 | 29 | if (! $allowed) { |
|
70 | 11 | $exception = InvalidStateTransitionException::notAllowedTransition( |
|
71 | 11 | $transitionName, |
|
72 | $context, |
||
73 | 11 | $this->currentState |
|
74 | ); |
||
75 | |||
76 | 11 | $this->listeners->dispatch( |
|
77 | 11 | StateEventStore::FAILURE_TRANSITION, |
|
78 | 11 | new TransitionWasFailed($transitionName, $exception) |
|
79 | ); |
||
80 | |||
81 | 11 | $newState = $callback->onFailure($exception, $context, $this); |
|
82 | } |
||
83 | |||
84 | 19 | $this->setCurrentState($newState); |
|
85 | |||
86 | 19 | $callback->afterStateChange($context, $this); |
|
87 | |||
88 | 19 | $this->listeners->dispatch( |
|
89 | 19 | StateEventStore::AFTER_TRANSITION, |
|
90 | 19 | new TransitionWasSuccessful($transitionName) |
|
91 | ); |
||
92 | |||
93 | 19 | return $this->currentState; |
|
94 | } |
||
0 ignored issues
–
show
|
|||
95 | |||
96 | /** |
||
0 ignored issues
–
show
|
|||
97 | * @param string $stateName |
||
0 ignored issues
–
show
|
|||
98 | * @return bool |
||
0 ignored issues
–
show
|
|||
99 | * @throws NotFoundException |
||
0 ignored issues
–
show
|
|||
100 | */ |
||
101 | 34 | public function isInState(string $stateName): bool |
|
102 | { |
||
0 ignored issues
–
show
|
|||
103 | 34 | if (! $this->states->hasState($stateName)) { |
|
104 | 1 | throw NotFoundException::stateNotFound($stateName); |
|
105 | } |
||
106 | |||
107 | 33 | return $this->currentState === $stateName; |
|
108 | } |
||
0 ignored issues
–
show
|
|||
109 | |||
110 | 9 | public function hasAttribute(string $attribute): bool |
|
0 ignored issues
–
show
|
|||
111 | { |
||
0 ignored issues
–
show
|
|||
112 | 9 | return $this->states->hasAttribute($this->currentState, $attribute); |
|
113 | } |
||
0 ignored issues
–
show
|
|||
114 | |||
115 | public function addListener(string $event, \Closure $listener): void |
||
0 ignored issues
–
show
|
|||
116 | { |
||
0 ignored issues
–
show
|
|||
117 | $this->listeners->addListener($event, $listener); |
||
118 | } |
||
0 ignored issues
–
show
|
|||
119 | |||
120 | 3 | public function acceptTransitionVisitor(TransitionVisitor $visitor): void |
|
0 ignored issues
–
show
|
|||
121 | { |
||
0 ignored issues
–
show
|
|||
122 | 3 | $this->states->acceptTransitionVisitor($visitor); |
|
123 | 3 | } |
|
0 ignored issues
–
show
|
|||
124 | |||
125 | 1 | public function acceptStateVisitor(StateVisitor $visitor): void |
|
0 ignored issues
–
show
|
|||
126 | { |
||
0 ignored issues
–
show
|
|||
127 | 1 | $this->states->acceptStateVisitor($visitor); |
|
128 | 1 | } |
|
0 ignored issues
–
show
|
|||
129 | |||
130 | 46 | private function setCurrentState(string $state): void |
|
0 ignored issues
–
show
|
|||
131 | { |
||
0 ignored issues
–
show
|
|||
132 | 46 | $this->currentState = $state; |
|
133 | 46 | } |
|
0 ignored issues
–
show
|
|||
134 | } |
||
0 ignored issues
–
show
|
|||
135 |