Completed
Push — master ( 923dd6...18a39e )
by Oliver
02:44
created

Transition::setWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Metabor\Statemachine;
4
5
use MetaborStd\Event\EventInterface;
6
use MetaborStd\Statemachine\ConditionInterface;
7
use MetaborStd\Statemachine\StateInterface;
8
use MetaborStd\Statemachine\TransitionInterface;
9
use MetaborStd\WeightedInterface;
10
11
/**
12
 * @author Oliver Tischlinger
13
 */
14
class Transition implements TransitionInterface, WeightedInterface
15
{
16
    /**
17
     * @var StateInterface
18
     */
19
    private $targetState;
20
21
    /**
22
     * @var string
23
     */
24
    private $eventName;
25
26
    /**
27
     * @var ConditionInterface
28
     */
29
    private $condition;
30
31
    /**
32
     * @var float
33
     */
34
    private $weight = 1;
35
36
    /**
37
     * @param StateInterface     $targetState
38
     * @param string             $eventName
39
     * @param ConditionInterface $condition
40
     */
41 20
    public function __construct(StateInterface $targetState, $eventName = null, ConditionInterface $condition = null)
42
    {
43 20
        $this->targetState = $targetState;
44 20
        $this->eventName = $eventName;
45 20
        $this->condition = $condition;
46 20
    }
47
48
    /**
49
     * @see MetaborStd\Statemachine.TransitionInterface::getTargetState()
50
     */
51 10
    public function getTargetState()
52
    {
53 10
        return $this->targetState;
54
    }
55
56
    /**
57
     * @see MetaborStd\Statemachine.TransitionInterface::getEventName()
58
     */
59 18
    public function getEventName()
60
    {
61 18
        return $this->eventName;
62
    }
63
64
    /**
65
     * @see MetaborStd\Statemachine.TransitionInterface::getConditionName()
66
     */
67 10
    public function getConditionName()
68
    {
69 10
        if ($this->condition) {
70 5
            return $this->condition->getName();
71
        }
72 10
    }
73
74
    /**
75
     * @see MetaborStd\Statemachine.TransitionInterface::isActive()
76
     */
77 3
    public function isActive($subject, \ArrayAccess $context, EventInterface $event = null)
78
    {
79 3
        if ($event) {
80 3
            $result = ($event->getName() == $this->eventName);
81 3
        } else {
82 2
            $result = is_null($this->eventName);
83
        }
84 3
        if ($this->condition) {
85 2
            $result = $result && $this->condition->checkCondition($subject, $context);
86 2
        }
87
88 3
        return $result;
89
    }
90
91
    /**
92
     * @return \MetaborStd\Statemachine\ConditionInterface
93
     */
94 3
    public function getCondition()
95
    {
96 3
        return $this->condition;
97
    }
98
99
    /**
100
     * @return float
101
     */
102 2
    public function getWeight()
103
    {
104 2
        return $this->weight;
105
    }
106
107
    /**
108
     * @param float $weight
109
     */
110 2
    public function setWeight($weight)
111
    {
112 2
        $this->weight = $weight;
113 2
    }
114
}
115