Passed
Pull Request — master (#19)
by Yannick
02:27
created

StateMetadata::getMachine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Star\Component\State;
4
5
use Star\Component\State\Builder\StateBuilder;
6
use Star\Component\State\Callbacks\TransitionCallback;
7
use Webmozart\Assert\Assert;
8
9
abstract class StateMetadata
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $current;
15
16
    /**
17
     * @param string $initial The initial state name
18
     */
19 15
    public function __construct($initial)
20
    {
21 15
        Assert::string($initial);
22 15
        $this->current = $initial;
23 15
    }
24
25
    /**
26
     * Returns the state workflow configuration.
27
     *
28
     * @param StateBuilder $builder
29
     */
30
    abstract protected function configure(StateBuilder $builder);
31
32 18
    private function getMachine()
33
    {
34 18
        $this->configure($builder = new StateBuilder());
35
36
        // todo implement caching for faster building
37 18
        return $builder->create($this->current);
38
    }
39
40
    /**
41
     * @param string $name
42
     * @param mixed $context
43
     * @param TransitionCallback|null $callback
44
     *
45
     * @return StateMetadata
46
     */
47 12
    final public function transit($name, $context, TransitionCallback $callback = null)
48
    {
49 12
        $this->current = $this->getMachine()->transit($name, $context, $callback);
50
51 11
        return $this;
52
    }
53
54
    /**
55
     * @param string $attribute
56
     *
57
     * @return bool
58
     */
59 6
    final public function hasAttribute($attribute)
60
    {
61 6
        return $this->getMachine()->hasAttribute($attribute);
62
    }
63
64
    /**
65
     * @param string $state
66
     *
67
     * @return bool
68
     */
69 16
    final public function isInState($state)
70
    {
71 16
        return $this->getMachine()->isInState($state);
72
    }
73
74
    /**
75
     * @return string
76
     */
77 1
    final public function getCurrent()
78
    {
79 1
        return $this->current;
80
    }
81
}
82