FsmTrait::checkFsmUpdate()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
c 0
b 0
f 0
rs 9.9666
cc 4
nc 2
nop 2
1
<?php
2
3
namespace SoliDry\Extension;
4
5
6
use SoliDry\Helpers\Json;
7
8
/**
9
 * Class FsmTrait
10
 *
11
 * @package SoliDry\Extension
12
 *
13
 * @property ApiController entity
14
 */
15
trait FsmTrait
16
{
17
    /**
18
     * @param array $jsonProps JSON input properties
19
     * @throws \SoliDry\Exceptions\AttributesException
20
     */
21
    protected function checkFsmCreate(array &$jsonProps) : void
22
    {
23
        $stateMachine = new StateMachine($this->entity);
0 ignored issues
show
Bug introduced by
$this->entity of type SoliDry\Extension\ApiController is incompatible with the type string expected by parameter $entity of SoliDry\Extension\StateMachine::__construct(). ( Ignorable by Annotation )

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

23
        $stateMachine = new StateMachine(/** @scrutinizer ignore-type */ $this->entity);
Loading history...
24
        $stateField   = $stateMachine->getField();
25
        if (empty($jsonProps[$stateField])) {
26
            $stateMachine->setInitial($stateField);
27
            $jsonProps[$stateField] = $stateMachine->getInitial();
28
        } else {
29
            foreach ($jsonProps as $k => $v) {
30
                if ($stateMachine->isStatedField($k) === true) {
31
                    $stateMachine->setStates($k);
32
                    if ($stateMachine->isInitial($v) === false) {
33
                        // the field is under state machine rules and it is not initial state
34
                        Json::outputErrors(
35
                            [
36
                                [
37
                                    JSONApiInterface::ERROR_TITLE  => 'This state is not an initial.',
38
                                    JSONApiInterface::ERROR_DETAIL => 'The state - \'' . $v . '\' is not an initial.',
39
                                ],
40
                            ]
41
                        );
42
                    }
43
                }
44
            }
45
        }
46
    }
47
48
    /**
49
     *
50
     * @param array $jsonProps
51
     * @param $model
52
     * @throws \SoliDry\Exceptions\AttributesException
53
     */
54
    protected function checkFsmUpdate(array $jsonProps, $model) : void
55
    {
56
        $stateMachine = new StateMachine($this->entity);
0 ignored issues
show
Bug introduced by
$this->entity of type SoliDry\Extension\ApiController is incompatible with the type string expected by parameter $entity of SoliDry\Extension\StateMachine::__construct(). ( Ignorable by Annotation )

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

56
        $stateMachine = new StateMachine(/** @scrutinizer ignore-type */ $this->entity);
Loading history...
57
        $field        = $stateMachine->getField();
58
        $stateMachine->setStates($field);
59
        if (empty($jsonProps[$field]) === false
60
            && $stateMachine->isStatedField($field) === true
61
            && $stateMachine->isTransitive($model->$field, $jsonProps[$field]) === false
62
        ) {
63
            // the field is under state machine rules and it is not transitive in this direction
64
            Json::outputErrors(
65
                [
66
                    [
67
                        JSONApiInterface::ERROR_TITLE  => 'State can`t be changed through this way.',
68
                        JSONApiInterface::ERROR_DETAIL => 'The state of a field/column - \'' . $field . '\' can`t be changed from: \'' . $model->$field . '\', to: \'' . $jsonProps[$field] . '\'',
69
                    ],
70
                ]
71
            );
72
        }
73
    }
74
}