Transition   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 10
eloc 20
dl 0
loc 50
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canTransitionFrom() 0 9 3
A __construct() 0 6 1
A name() 0 3 1
A toStateName() 0 3 1
A cannotTransitionFrom() 0 9 3
A metadata() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FiniteStateMachine\Transition;
6
7
use Dflydev\FiniteStateMachine\State\State;
8
9
class Transition
10
{
11
    private string $name;
12
    private string $toStateName;
13
    private array $fromStateNames;
14
    private array $metadata;
15
16 21
    public function __construct(string $name, string $toStateName, array $fromStateNames, array $metadata = [])
17
    {
18 21
        $this->name = $name;
19 21
        $this->toStateName = $toStateName;
20 21
        $this->fromStateNames = $fromStateNames;
21 21
        $this->metadata = $metadata;
22 21
    }
23
24 21
    public function name(): string
25
    {
26 21
        return $this->name;
27
    }
28
29 2
    public function metadata(): array
30
    {
31 2
        return $this->metadata;
32
    }
33
34 13
    public function toStateName(): string
35
    {
36 13
        return $this->toStateName;
37
    }
38
39 8
    public function canTransitionFrom(State $state): bool
40
    {
41 8
        foreach ($this->fromStateNames as $fromStateName) {
42 8
            if ($state->name() === $fromStateName) {
43 7
                return true;
44
            }
45
        }
46
47 8
        return false;
48
    }
49
50 13
    public function cannotTransitionFrom(State $state): bool
51
    {
52 13
        foreach ($this->fromStateNames as $fromStateName) {
53 13
            if ($state->name() === $fromStateName) {
54 13
                return false;
55
            }
56
        }
57
58
        return true;
59
    }
60
}
61