Originator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 51
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A undo() 0 5 1
A changeState() 0 5 1
A save() 0 3 1
A state() 0 3 1
1
<?php
2
3
namespace Tleckie\DesignPatterns\Memento;
4
5
/**
6
 * Class Originator
7
 *
8
 * @package Tleckie\DesignPatterns\Memento
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class Originator
12
{
13
    /** @var State */
14
    private State $state;
15
16
    /**
17
     * Originator constructor.
18
     *
19
     * @param State $state
20
     */
21
    public function __construct(State $state)
22
    {
23
        $this->changeState($state);
24
    }
25
26
    /**
27
     * @param State $state
28
     * @return $this
29
     */
30
    public function changeState(State $state): Originator
31
    {
32
        $this->state = $state;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @return Memento
39
     */
40
    public function save(): Memento
41
    {
42
        return new Memento($this->state);
43
    }
44
45
    /**
46
     * @return State
47
     */
48
    public function state(): State
49
    {
50
        return $this->state;
51
    }
52
53
    /**
54
     * @param Memento $memento
55
     * @return Memento
56
     */
57
    public function undo(Memento $memento): Memento
58
    {
59
        $this->changeState($memento->state());
60
61
        return $memento;
62
    }
63
}
64