Process   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
dl 0
loc 103
c 6
b 0
f 2
wmc 8
lcom 1
cbo 5
ccs 19
cts 19
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialState() 0 4 1
A __construct() 0 15 3
A name() 0 4 1
A state() 0 4 1
A states() 0 4 1
A triggerEvent() 0 4 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\States;
17
use StateMachine\Exception\InvalidArgumentException;
18
use StateMachine\Exception\InvalidStateException;
19
use StateMachine\Payload;
20
21
/**
22
 * State machine process
23
 *
24
 * @package StateMachine
25
 */
26
final class Process implements ProcessInterface
27
{
28
    /**
29
     * Process/schema name
30
     *
31
     * @var string
32
     */
33
    private $name;
34
35
    /**
36
     * Initial state
37
     *
38
     * @var string
39
     */
40
    private $initialState;
41
42
    /**
43
     * Schema states
44
     *
45
     * @var States|State[]
46
     */
47
    private $states = [];
48
49
    /**
50
     * @param string  $name         process/schema name
51
     * @param string  $initialState initial state for entities starting process
52
     * @param State[] $states
53
     *
54
     * @throws InvalidArgumentException|InvalidStateException
55
     */
56 11
    public function __construct($name, $initialState, array $states)
57
    {
58 11
        if (empty($name)) {
59 1
            throw InvalidArgumentException::emptyProcessName();
60
        }
61
62 10
        $this->name = $name;
63 10
        $this->initialState = $initialState;
64
65 10
        $this->states = new States($states);
66
67 10
        if (!$this->states->has($this->initialState)) {
68 1
            throw InvalidStateException::missingInitialState($this->name, $this->initialState);
69
        }
70 9
    }
71
72
    /**
73
     * Return process name
74
     *
75
     * @return string
76
     */
77 1
    public function name(): string
78
    {
79 1
        return $this->name;
80
    }
81
82
    /**
83
     * Return initial state
84
     * All entities without state will have this one
85
     *
86
     * @return State
87
     */
88 5
    public function initialState(): State
89
    {
90 5
        return $this->state($this->initialState);
91
    }
92
93
    /**
94
     * Return state from collection by its name
95
     *
96
     * @param string $name
97
     *
98
     * @return State
99
     */
100 6
    public function state($name): State
101
    {
102 6
        return $this->states->get($name);
103
    }
104
105
    /**
106
     * Return all states
107
     *
108
     * @return State[]
109
     */
110 1
    public function states(): array
111
    {
112 1
        return $this->states->all();
113
    }
114
115
    /**
116
     * Trigger event for payload
117
     * Return next state name
118
     *
119
     * @param string  $event
120
     * @param Payload $payload
121
     *
122
     * @return string
123
     */
124 1
    public function triggerEvent($event, Payload $payload): string
125
    {
126 1
        return $this->state($payload->state())->triggerEvent($event, $payload);
127
    }
128
}
129