State::hasEvent()   A
last analyzed

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
declare(strict_types = 1);
4
5
/*
6
* This file is part of the statemachine package
7
*
8
* (c) Michal Wachowski <[email protected]>
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
14
namespace StateMachine;
15
16
use StateMachine\Collection\Events;
17
use StateMachine\Exception\InvalidArgumentException;
18
use StateMachine\Payload;
19
20
/**
21
 * State machine state representation
22
 *
23
 * @package StateMachine
24
 */
25
final class State
26
{
27
    /**
28
     * State name
29
     *
30
     * @var string
31
     */
32
    private $name;
33
34
    /**
35
     * State events
36
     *
37
     * @var Events
38
     */
39
    private $events;
40
41
    /**
42
     * Additional attributes
43
     *
44
     * @var Attributes
45
     */
46
    private $attributes;
47
48
    /**
49
     * @param string  $name       state name
50
     * @param Event[] $events     list of events in state
51
     * @param array   $attributes additional attributes
52
     *
53
     * @throws InvalidArgumentException
54
     */
55 26
    public function __construct($name, array $events = [], array $attributes = [])
56
    {
57 26
        if (empty($name)) {
58 1
            throw InvalidArgumentException::emptyStateName();
59
        }
60
61 25
        $this->name = $name;
62 25
        $this->events = new Events($events);
63 25
        $this->attributes = new Attributes($attributes);
64 25
    }
65
66
    /**
67
     * Return state name
68
     *
69
     * @return string
70
     */
71 16
    public function name(): string
72
    {
73 16
        return $this->name;
74
    }
75
76
    /**
77
     * Return event collection
78
     *
79
     * @return Event[]
80
     */
81 1
    public function events(): array
82
    {
83 1
        return $this->events->all();
84
    }
85
86
    /**
87
     * Return true if event exists in collection
88
     *
89
     * @param string $name
90
     *
91
     * @return bool
92
     */
93 5
    public function hasEvent($name): bool
94
    {
95 5
        return $this->events->has($name);
96
    }
97
98
    /**
99
     * Return event with given name
100
     *
101
     * @param string $name
102
     *
103
     * @return Event
104
     */
105 8
    public function event($name): Event
106
    {
107 8
        return $this->events->get($name);
108
    }
109
110
    /**
111
     * Return attributes container
112
     *
113
     * @return Attributes
114
     */
115 1
    public function attributes(): Attributes
116
    {
117 1
        return $this->attributes;
118
    }
119
120
    /**
121
     * Triggers event with given name and payload
122
     * Returns name of next state or null if no change
123
     *
124
     * @param string  $name
125
     * @param Payload $payload
126
     *
127
     * @return null|string
128
     */
129 6
    public function triggerEvent($name, Payload $payload)
130
    {
131 6
        return $this->event($name)->trigger($payload);
132
    }
133
134
    /**
135
     * Return state string representation - its name
136
     *
137
     * @return string
138
     */
139 15
    public function __toString(): string
140
    {
141 15
        return $this->name();
142
    }
143
}
144