Issues (1124)

src/StateMetadata.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace Star\Component\State;
4
5
use Star\Component\State\Builder\StateBuilder;
6
use Star\Component\State\Callbacks\TransitionCallback;
7
8
abstract class StateMetadata
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $current;
14
15
    /**
16
     * @param string $initial The initial state name
17
     */
18 15
    public function __construct(string $initial)
19
    {
20 15
        $this->current = $initial;
21 15
    }
22
23
    /**
24
     * Returns the state workflow configuration.
25
     *
26
     * @param StateBuilder $builder
27
     */
28
    abstract protected function configure(StateBuilder $builder): void;
29
30 18
    private function getMachine(): StateMachine
31
    {
32 18
        $this->configure($builder = new StateBuilder());
33
34
        // todo implement caching for faster building
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
35 18
        return $builder->create($this->current);
36
    }
37
38
    /**
39
     * @param string $name
40
     * @param mixed $context
41
     * @param TransitionCallback|null $callback
42
     *
43
     * @return static
44
     */
45 12
    final public function transit(string $name, $context, TransitionCallback $callback = null): StateMetadata
46
    {
47 12
        $this->current = $this->getMachine()->transit($name, $context, $callback);
48
49 11
        return $this;
50
    }
51
52 6
    final public function hasAttribute(string $attribute): bool
53
    {
54 6
        return $this->getMachine()->hasAttribute($attribute);
55
    }
56
57 16
    final public function isInState(string $state): bool
58
    {
59 16
        return $this->getMachine()->isInState($state);
60
    }
61
62 1
    final public function getCurrent(): string
63
    {
64 1
        return $this->current;
65
    }
66
}
67