StateMachine   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 21
dl 0
loc 85
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getInitial() 0 3 1
A setInitial() 0 7 2
A setStates() 0 7 2
A __construct() 0 4 1
A isInitial() 0 4 2
A getField() 0 3 1
A isStatedField() 0 5 3
A isTransitive() 0 3 2
1
<?php
2
namespace SoliDry\Extension;
3
4
use SoliDry\Exceptions\AttributesException;
5
use SoliDry\Helpers\ConfigHelper;
6
use SoliDry\Helpers\MigrationsHelper;
7
use SoliDry\Types\ConfigInterface;
8
use SoliDry\Types\ErrorsInterface;
9
10
/**
11
 * Class StateMachine
12
 * @package SoliDry\Extension
13
 */
14
class StateMachine
15
{
16
    private $machine = [];
17
    private $states  = [];
18
    private $initial;
19
    // state field taken from table in config
20
    private $field;
21
22
    /**
23
     * StateMachine constructor.
24
     * @param string $entity
25
     */
26
    public function __construct(string $entity)
27
    {
28
        $this->machine = ConfigHelper::getNestedParam(ConfigInterface::STATE_MACHINE, MigrationsHelper::getTableName($entity));
29
        $this->field   = key($this->machine);
0 ignored issues
show
Bug introduced by
It seems like $this->machine can also be of type null; however, parameter $array of key() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $this->field   = key(/** @scrutinizer ignore-type */ $this->machine);
Loading history...
30
    }
31
32
    /**
33
     * @param string $field
34
     * @return bool
35
     */
36
    public function isStatedField(string $field) : bool
37
    {
38
        return empty($this->machine[$field]) === false
39
            && $this->machine[$field][ConfigInterface::ENABLED] === true
40
            && empty($this->machine[$field][ConfigInterface::STATES]) === false;
41
    }
42
43
    /**
44
     * @param mixed $from
45
     * @param mixed $to
46
     * @return bool
47
     */
48
    public function isTransitive($from, $to): bool
49
    {
50
        return $from === $to || in_array($to, $this->states[$from], true);
51
    }
52
53
    public function isInitial($state): bool
54
    {
55
        return empty($this->states[ConfigInterface::INITIAL]) === false
56
        && in_array($state, $this->states[ConfigInterface::INITIAL]);
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getInitial()
63
    {
64
        return $this->initial;
65
    }
66
67
    /**
68
     * @param string $field
69
     * @throws AttributesException
70
     */
71
    public function setStates(string $field) : void
72
    {
73
        if(empty($this->machine[$field][ConfigInterface::STATES])) {
74
            throw new AttributesException(ErrorsInterface::JSON_API_ERRORS[ErrorsInterface::HTTP_CODE_BULK_EXT_ERROR], ErrorsInterface::HTTP_CODE_BULK_EXT_ERROR);
75
        }
76
77
        $this->states = $this->machine[$field][ConfigInterface::STATES];
78
    }
79
80
    /**
81
     * @param string $field
82
     * @throws AttributesException
83
     */
84
    public function setInitial(string $field) : void
85
    {
86
        if(empty($this->machine[$field][ConfigInterface::STATES][ConfigInterface::INITIAL][0])) {
87
            throw new AttributesException('There should be an initial value for: "' . $field . '" field."', ErrorsInterface::HTTP_CODE_FSM_INIT_ATTR);
88
        }
89
90
        $this->initial = $this->machine[$field][ConfigInterface::STATES][ConfigInterface::INITIAL][0];
91
    }
92
93
    /**
94
     * @return mixed|null
95
     */
96
    public function getField()
97
    {
98
        return $this->field;
99
    }
100
}