Completed
Push — master ( 5bce4a...92d5e1 )
by Yohan
03:01
created

TransitionEvent::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 6
Bugs 1 Features 3
Metric Value
c 6
b 1
f 3
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 11
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Finite\Event;
4
5
use Finite\State\StateInterface;
6
use Finite\StateMachine\StateMachine;
7
use Finite\Transition\PropertiesAwareTransitionInterface;
8
use Finite\Transition\TransitionInterface;
9
10
/**
11
 * The event object which is thrown on transitions actions.
12
 *
13
 * @author Yohan Giarelli <[email protected]>
14
 */
15
class TransitionEvent extends StateMachineEvent
16
{
17
    /**
18
     * @var TransitionInterface
19
     */
20
    protected $transition;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $transitionRejected = false;
26
27
    /**
28
     * @var StateInterface
29
     */
30
    protected $initialState;
31
32
    /**
33
     * @var array
34
     */
35
    protected $properties;
36
37
    /**
38
     * @param StateInterface      $initialState
39
     * @param TransitionInterface $transition
40
     * @param StateMachine        $stateMachine
41
     * @param array               $properties
42
     */
43 75
    public function __construct(
44
        StateInterface $initialState,
45
        TransitionInterface $transition,
46
        StateMachine $stateMachine,
47
        array $properties = array()
48
    ) {
49 75
        $this->transition = $transition;
50 75
        $this->initialState = $initialState;
51 75
        $this->properties = $properties;
52
53 75
        if ($transition instanceof PropertiesAwareTransitionInterface) {
54 70
            $this->properties = $transition->resolveProperties($properties);
55 56
        }
56
57 75
        parent::__construct($stateMachine);
58 75
    }
59
60
    /**
61
     * @return TransitionInterface
62
     */
63 20
    public function getTransition()
64
    {
65 20
        return $this->transition;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 50
    public function isRejected()
72
    {
73 50
        return $this->transitionRejected;
74
    }
75
76 5
    public function reject()
77
    {
78 5
        $this->transitionRejected = true;
79 5
    }
80
81
    /**
82
     * @return StateInterface
83
     */
84 20
    public function getInitialState()
85
    {
86 20
        return $this->initialState;
87
    }
88
89
    /**
90
     * @param string $property
91
     *
92
     * @return bool
93
     */
94 5
    public function has($property)
95
    {
96 5
        return array_key_exists($property, $this->properties);
97
    }
98
99
    /**
100
     * @param string $property
101
     * @param mixed  $default
102
     *
103
     * @return mixed
104
     */
105 5
    public function get($property, $default = null)
106
    {
107 5
        return $this->has($property) ? $this->properties[$property] : $default;
108
    }
109
110
    /**
111
     * @return array
112
     */
113 5
    public function getProperties()
114
    {
115 5
        return $this->properties;
116
    }
117
}
118