History::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tleckie\DesignPatterns\Memento;
4
5
use function array_pop;
6
use function count;
7
8
/**
9
 * Class History
10
 *
11
 * @package Tleckie\DesignPatterns\Memento
12
 * @author  Teodoro Leckie Westberg <[email protected]>
13
 */
14
class History
15
{
16
    /** @var Memento[] */
17
    private array $mementoList;
18
19
    /** @var Originator */
20
    private Originator $originator;
21
22
    /**
23
     * History constructor.
24
     *
25
     * @param Originator $originator
26
     */
27
    public function __construct(Originator $originator)
28
    {
29
        $this->originator = $originator;
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public function save(): History
36
    {
37
        $this->mementoList[] = $this->originator->save();
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return $this
44
     */
45
    public function undo(): History
46
    {
47
        if (!count($this->mementoList)) {
48
            return $this;
49
        }
50
51
        $this->originator->undo(array_pop($this->mementoList));
52
53
        return $this;
54
    }
55
}
56