Completed
Push — master ( 78e074...fc63d4 )
by Yohan
03:17
created

Transition::resolveProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Finite\Transition;
4
5
use Finite\StateMachine\StateMachineInterface;
6
use Finite\State\StateInterface;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * The base Transition class.
11
 * Feel free to extend it to fit to your needs
12
 *
13
 * @author Yohan Giarelli <[email protected]>
14
 * @author Michal Dabrowski <[email protected]>
15
 */
16
class Transition implements TransitionInterface, PropertiesAwareTransitionInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $initialStates;
22
23
    /*
24
     * @var string
25
     */
26
    protected $state;
27
28
    /**
29
     * @var string
30
     */
31
    protected $name;
32
33
    /**
34
     * @var callable
35
     */
36
    protected $guard;
37
38
    /**
39
     * @var OptionsResolver
40
     */
41
    protected $propertiesOptionsResolver;
42
43
    /**
44
     * @param string          $name
45
     * @param string|array    $initialStates
46
     * @param string          $state
47
     * @param callable|null   $guard
48
     * @param OptionsResolver $propertiesOptionsResolver
49
     */
50 165
    public function __construct(
51
        $name,
52
        $initialStates,
53
        $state,
54
        $guard = null,
55
        OptionsResolver $propertiesOptionsResolver = null
56
    ) {
57 165
        if (null !== $guard && !is_callable($guard)) {
58
            throw new \InvalidArgumentException('Invalid callable guard argument passed to Transition::__construct().');
59
        }
60
61 165
        $this->name = $name;
62 165
        $this->state = $state;
63 165
        $this->initialStates = (array) $initialStates;
64 165
        $this->guard = $guard;
65 165
        $this->propertiesOptionsResolver = $propertiesOptionsResolver ?: new OptionsResolver;
66 165
    }
67
68
    /**
69
     * @param string|StateInterface $state
70
     */
71
    public function addInitialState($state)
72
    {
73
        if ($state instanceof StateInterface) {
74
            $state = $state->getName();
75
        }
76
77
        $this->initialStates[] = $state;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 145
    public function getInitialStates()
84
    {
85 145
        return $this->initialStates;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 145
    public function getState()
92
    {
93 145
        return $this->state;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 15
    public function process(StateMachineInterface $stateMachine)
100
    {
101 15
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 145
    public function getName()
107
    {
108 145
        return $this->name;
109
    }
110
111
    /**
112
     * @return callable|null
113
     */
114 40
    public function getGuard()
115
    {
116 40
        return $this->guard;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122 45
    public function resolveProperties(array $properties)
123
    {
124 45
        return $this->propertiesOptionsResolver->resolve($properties);
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function __toString()
131
    {
132
        return $this->getName();
133
    }
134
}
135