|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace App\Services\LogSystem; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
|
25
|
|
|
|
|
26
|
|
|
class EventUndoHelper |
|
27
|
|
|
{ |
|
28
|
|
|
public const MODE_UNDO = 'undo'; |
|
29
|
|
|
public const MODE_REVERT = 'revert'; |
|
30
|
|
|
|
|
31
|
|
|
protected const ALLOWED_MODES = [self::MODE_REVERT, self::MODE_UNDO]; |
|
32
|
|
|
|
|
33
|
|
|
protected $undone_event; |
|
34
|
|
|
protected $mode; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$undone_event = null; |
|
|
|
|
|
|
39
|
|
|
$this->mode = self::MODE_UNDO; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setMode(string $mode): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (!in_array($mode, self::ALLOWED_MODES)) { |
|
45
|
|
|
throw new \InvalidArgumentException('Invalid mode passed!'); |
|
46
|
|
|
} |
|
47
|
|
|
$this->mode = $mode; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getMode(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->mode; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set which event log is currently undone. |
|
57
|
|
|
* After the flush this message is cleared. |
|
58
|
|
|
* @param AbstractLogEntry|null $undone_event |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setUndoneEvent(?AbstractLogEntry $undone_event): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->undone_event = $undone_event; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns event that is currently undone. |
|
67
|
|
|
* @return AbstractLogEntry|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getUndoneEvent(): ?AbstractLogEntry |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->undone_event; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Clear the currently the set undone event. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function clearUndoneEvent(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->undone_event = null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if a event is undone |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isUndo(): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return ($this->undone_event instanceof AbstractLogEntry); |
|
89
|
|
|
} |
|
90
|
|
|
} |