PayloadEnvelope::hasChanged()   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 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Payload;
17
18
/**
19
 * Payload envelope, used to transport subject trough state machine process
20
 *
21
 * @package StateMachine
22
 */
23
final class PayloadEnvelope implements Payload
24
{
25
    /**
26
     * Flag if context changed
27
     *
28
     * @var bool
29
     */
30
    private $hasChanged = false;
31
32
    /**
33
     * Current contexts state
34
     *
35
     * @var string
36
     */
37
    private $state;
38
39
    /**
40
     * Current runs history
41
     *
42
     * @var array
43
     */
44
    private $history = [];
45
46
    /**
47
     * Actual context instance
48
     *
49
     * @var mixed
50
     */
51
    private $subject;
52
53 16
    private function __construct($subject)
54
    {
55 16
        $this->subject = $subject;
56 16
    }
57
58
    /**
59
     * @param mixed $subject
60
     *
61
     * @return PayloadEnvelope
62
     */
63 16
    public static function wrap($subject): PayloadEnvelope
64
    {
65 16
        return new static($subject);
66
    }
67
68
    /**
69
     * Return true if state has changed (even if it was changed back to itself)
70
     *
71
     * @return bool
72
     */
73 1
    public function hasChanged(): bool
74
    {
75 1
        return $this->hasChanged;
76
    }
77
78
    /**
79
     * Return current subject state
80
     *
81
     * @return null|string
82
     */
83 6
    public function state()
84
    {
85 6
        return $this->state;
86
    }
87
88
    /**
89
     * Set new state to subject
90
     *
91
     * @param string $name state name
92
     */
93 8
    public function changeState($name)
94
    {
95 8
        $this->state = $name;
96 8
        $this->history[] = $name;
97 8
        $this->hasChanged = true;
98 8
    }
99
100
    /**
101
     * Return array with all states that were set in this run
102
     *
103
     * @return array
104
     */
105 5
    public function history(): array
106
    {
107 5
        return $this->history;
108
    }
109
110
    /**
111
     * Return subject
112
     *
113
     * @return mixed
114
     */
115 1
    public function subject()
116
    {
117 1
        return $this->subject;
118
    }
119
}
120