yvoyer /
php-state
| 1 | <?php declare(strict_types=1); |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | namespace Star\Component\State\Builder; |
||
| 4 | |||
| 5 | use Star\Component\State\EventRegistry; |
||
| 6 | use Star\Component\State\Port\Symfony\EventDispatcherAdapter; |
||
| 7 | use Star\Component\State\StateMachine; |
||
| 8 | use Star\Component\State\StateTransition; |
||
| 9 | use Star\Component\State\TransitionRegistry; |
||
| 10 | use Star\Component\State\Transitions\ManyToOneTransition; |
||
| 11 | use Star\Component\State\Transitions\OneToOneTransition; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Tool to build the StateMachine. |
||
| 15 | */ |
||
|
0 ignored issues
–
show
|
|||
| 16 | final class StateBuilder |
||
| 17 | { |
||
|
0 ignored issues
–
show
|
|||
| 18 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @var TransitionRegistry |
||
| 20 | */ |
||
| 21 | private $registry; |
||
|
0 ignored issues
–
show
|
|||
| 22 | |||
| 23 | /** |
||
|
0 ignored issues
–
show
|
|||
| 24 | * @var EventRegistry |
||
| 25 | */ |
||
| 26 | private $listeners; |
||
|
0 ignored issues
–
show
|
|||
| 27 | |||
| 28 | 36 | public function __construct() |
|
|
0 ignored issues
–
show
|
|||
| 29 | { |
||
|
0 ignored issues
–
show
|
|||
| 30 | 36 | $this->registry = new TransitionRegistry(); |
|
|
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. Loading history...
|
|||
| 31 | 36 | $this->listeners = new EventDispatcherAdapter(); |
|
| 32 | 36 | } |
|
|
0 ignored issues
–
show
|
|||
| 33 | |||
| 34 | /** |
||
|
0 ignored issues
–
show
|
|||
| 35 | * @param string $name |
||
|
0 ignored issues
–
show
|
|||
| 36 | * @param string|string[] $from |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @param string $to |
||
|
0 ignored issues
–
show
|
|||
| 38 | * |
||
| 39 | * @return StateBuilder |
||
| 40 | */ |
||
| 41 | 36 | public function allowTransition(string $name, $from, string $to): StateBuilder |
|
| 42 | { |
||
|
0 ignored issues
–
show
|
|||
| 43 | 36 | if (\is_array($from)) { |
|
| 44 | 16 | $transition = new ManyToOneTransition($name, $to, ...$from); |
|
| 45 | } else { |
||
| 46 | 36 | $transition = new OneToOneTransition($name, $from, $to); |
|
| 47 | } |
||
| 48 | |||
| 49 | 36 | $this->allowCustomTransition($transition); |
|
| 50 | |||
| 51 | 36 | return $this; |
|
| 52 | } |
||
|
0 ignored issues
–
show
|
|||
| 53 | |||
| 54 | /** |
||
|
0 ignored issues
–
show
|
|||
| 55 | * @param StateTransition $transition |
||
|
0 ignored issues
–
show
|
|||
| 56 | */ |
||
|
0 ignored issues
–
show
|
|||
| 57 | 36 | public function allowCustomTransition(StateTransition $transition): void |
|
| 58 | { |
||
|
0 ignored issues
–
show
|
|||
| 59 | 36 | $this->registry->addTransition($transition); |
|
| 60 | 36 | } |
|
|
0 ignored issues
–
show
|
|||
| 61 | |||
| 62 | /** |
||
|
0 ignored issues
–
show
|
|||
| 63 | * @param string $attribute The attribute |
||
|
0 ignored issues
–
show
|
|||
| 64 | * @param string|string[] $states The list of states that this attribute applies to |
||
|
0 ignored issues
–
show
|
|||
| 65 | * |
||
| 66 | * @return StateBuilder |
||
| 67 | */ |
||
| 68 | 31 | public function addAttribute(string $attribute, $states): StateBuilder |
|
| 69 | { |
||
|
0 ignored issues
–
show
|
|||
| 70 | 31 | $states = (array) $states; |
|
|
0 ignored issues
–
show
|
|||
| 71 | 31 | foreach ($states as $stateName) { |
|
| 72 | 31 | $this->registry->addAttribute($stateName, $attribute); |
|
| 73 | } |
||
| 74 | |||
| 75 | 31 | return $this; |
|
| 76 | } |
||
|
0 ignored issues
–
show
|
|||
| 77 | |||
| 78 | 36 | public function create(string $currentState): StateMachine |
|
|
0 ignored issues
–
show
|
|||
| 79 | { |
||
|
0 ignored issues
–
show
|
|||
| 80 | 36 | return new StateMachine($currentState, $this->registry, $this->listeners); |
|
| 81 | } |
||
|
0 ignored issues
–
show
|
|||
| 82 | |||
| 83 | 18 | public static function build(): StateBuilder |
|
|
0 ignored issues
–
show
|
|||
| 84 | { |
||
|
0 ignored issues
–
show
|
|||
| 85 | 18 | return new static(); |
|
| 86 | } |
||
|
0 ignored issues
–
show
|
|||
| 87 | } |
||
|
0 ignored issues
–
show
|
|||
| 88 |