Completed
Pull Request — dev (#7)
by Michal
06:43
created

PayloadEnvelope   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 110
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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