State::getEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Metabor\Statemachine;
4
5
use Metabor\Event\Event;
6
use Metabor\KeyValue\Nullable;
7
use Metabor\Named;
8
use Metabor\NamedCollection;
9
use Metabor\Statemachine\Util\ArrayAccessToArrayConverter;
10
use MetaborStd\MetadataInterface;
11
use MetaborStd\Statemachine\StateInterface;
12
use MetaborStd\Statemachine\TransitionInterface;
13
14
/**
15
 * @author Oliver Tischlinger
16
 */
17
class State extends Named implements StateInterface, \ArrayAccess, MetadataInterface
18
{
19
    /**
20
     * @var \SplObjectStorage
21
     */
22
    private $transitions;
23
24
    /**
25
     * @var NamedCollection
26
     */
27
    private $events;
28
29
    /**
30
     * @var \ArrayAccess
31
     */
32
    private $metadata;
33
34
    /**
35
     * @param string $name
36
     */
37 34
    public function __construct($name)
38
    {
39 34
        parent::__construct($name);
40 34
        $this->transitions = new \SplObjectStorage();
41 34
        $this->events = new NamedCollection();
42 34
        $this->metadata = new Nullable();
43 34
    }
44
45
    /**
46
     * @return \Traversable
47
     */
48 22
    public function getTransitions()
49
    {
50 22
        return clone $this->transitions;
51
    }
52
53
    /**
54
     * @param TransitionInterface $transition
55
     */
56 14
    public function addTransition(TransitionInterface $transition)
57
    {
58 14
        $this->transitions->attach($transition);
59 14
        $eventName = $transition->getEventName();
60 14
        if ($eventName) {
61 13
            $this->getEvent($eventName);
62 13
        }
63 14
    }
64
65
    /**
66
     * @see MetaborStd\Statemachine.StateInterface::getEventNames()
67
     */
68 4
    public function getEventNames()
69
    {
70 4
        return $this->events->getNames();
71
    }
72
73
    /**
74
     * @see MetaborStd\Statemachine.StateInterface::hasEvent()
75
     */
76 6
    public function hasEvent($name)
77
    {
78 6
        return $this->events->has($name);
79
    }
80
81
    /**
82
     * @see MetaborStd\Statemachine.StateInterface::getEvent()
83
     */
84 13
    public function getEvent($name)
85
    {
86 13
        if ($this->events->has($name)) {
87 6
            $event = $this->events->get($name);
88 6
        } else {
89 13
            $event = new Event($name);
90 13
            $this->events->add($event);
91
        }
92
93 13
        return $event;
94
    }
95
96
    /**
97
     * @see ArrayAccess::offsetExists()
98
     */
99 2
    public function offsetExists($offset)
100
    {
101 2
        return $this->metadata->offsetExists($offset);
102
    }
103
104
    /**
105
     * @see ArrayAccess::offsetGet()
106
     */
107 2
    public function offsetGet($offset)
108
    {
109 2
        return $this->metadata->offsetGet($offset);
110
    }
111
112
    /**
113
     * @see ArrayAccess::offsetSet()
114
     */
115 4
    public function offsetSet($offset, $value)
116
    {
117 4
        $this->metadata->offsetSet($offset, $value);
118 4
    }
119
120
    /**
121
     * @see ArrayAccess::offsetUnset()
122
     */
123 1
    public function offsetUnset($offset)
124
    {
125 1
        $this->metadata->offsetUnset($offset);
126 1
    }
127
128
    /**
129
     * @see \MetaborStd\MetadataInterface::getMetadata()
130
     */
131 4
    public function getMetadata()
132
    {
133 4
        $converter = new ArrayAccessToArrayConverter($this->metadata);
134
135 4
        return $converter->toArray();
136
    }
137
}
138