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

TransitionEvent::has()   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\Event;
4
5
use Finite\Exception\TransitionException;
6
use Finite\State\StateInterface;
7
use Finite\StateMachine\StateMachine;
8
use Finite\Transition\PropertiesAwareTransitionInterface;
9
use Finite\Transition\TransitionInterface;
10
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
11
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
12
13
/**
14
 * The event object which is thrown on transitions actions
15
 *
16
 * @author Yohan Giarelli <[email protected]>
17
 */
18
class TransitionEvent extends StateMachineEvent
19
{
20
    /**
21
     * @var TransitionInterface
22
     */
23
    protected $transition;
24
25
    /**
26
     * @var boolean
27
     */
28
    protected $transitionRejected = false;
29
30
    /**
31
     * @var StateInterface
32
     */
33
    protected $initialState;
34
35
    /**
36
     * @var array
37
     */
38
    protected $properties;
39
40
    /**
41
     * @param StateInterface      $initialState
42
     * @param TransitionInterface $transition
43
     * @param StateMachine        $stateMachine
44
     * @param array               $properties
45
     *
46
     * @throws TransitionException
47
     */
48 55
    public function __construct(
49
        StateInterface $initialState,
50
        TransitionInterface $transition,
51
        StateMachine $stateMachine,
52
        array $properties = array()
53
    ) {
54 55
        $this->transition   = $transition;
55 55
        $this->initialState = $initialState;
56 55
        $this->properties   = $properties;
57
58 55
        if ($transition instanceof PropertiesAwareTransitionInterface) {
59
            try {
60 50
                $this->properties = $transition->resolveProperties($properties);
61 40
            } catch (MissingOptionsException $e) {
62
                throw new TransitionException(
63
                    'Testing or applying this transition need a parameter. Provide it or set it optional.',
64
                    $e->getCode(),
65
                    $e
66
                );
67
            } catch (UndefinedOptionsException $e) {
68
                throw new TransitionException(
69
                    'You provided an unknown property to test() or apply(). Remove it or declare it in your graph.',
70
                    $e->getCode(),
71
                    $e
72
                );
73
            }
74 40
        }
75
76 55
        parent::__construct($stateMachine);
77 55
    }
78
79
    /**
80
     * @return TransitionInterface
81
     */
82
    public function getTransition()
83
    {
84
        return $this->transition;
85
    }
86
87
    /**
88
     * @return bool
89
     */
90 45
    public function isRejected()
91
    {
92 45
        return $this->transitionRejected;
93
    }
94
95 5
    public function reject()
96
    {
97 5
        $this->transitionRejected = true;
98 5
    }
99
100
    /**
101
     * @return StateInterface
102
     */
103
    public function getInitialState()
104
    {
105
        return $this->initialState;
106
    }
107
108
    /**
109
     * @param string $property
110
     *
111
     * @return bool
112
     */
113 5
    public function has($property)
114
    {
115 5
        return array_key_exists($property, $this->properties);
116
    }
117
118
    /**
119
     * @param string $property
120
     * @param mixed  $default
121
     *
122
     * @return mixed
123
     */
124 5
    public function get($property, $default = null)
125
    {
126 5
        return $this->has($property) ? $this->properties[$property] : $default;
127
    }
128
129
    /**
130
     * @return array
131
     */
132 5
    public function getProperties()
133
    {
134 5
        return $this->properties;
135
    }
136
}
137