Use enum for undo mode

This commit is contained in:
Jan Böhmer 2023-06-18 21:26:28 +02:00
parent 218b0adb8f
commit 8a20584e27
14 changed files with 140 additions and 180 deletions

View file

@ -46,27 +46,19 @@ use InvalidArgumentException;
class EventUndoHelper
{
final public const MODE_UNDO = 'undo';
final public const MODE_REVERT = 'revert';
protected const ALLOWED_MODES = [self::MODE_REVERT, self::MODE_UNDO];
protected ?AbstractLogEntry $undone_event = null;
protected string $mode = self::MODE_UNDO;
protected EventUndoMode $mode = EventUndoMode::UNDO;
public function __construct()
{
}
public function setMode(string $mode): void
public function setMode(EventUndoMode $mode): void
{
if (!in_array($mode, self::ALLOWED_MODES, true)) {
throw new InvalidArgumentException('Invalid mode passed!');
}
$this->mode = $mode;
}
public function getMode(): string
public function getMode(): EventUndoMode
{
return $this->mode;
}

View file

@ -0,0 +1,46 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Services\LogSystem;
use InvalidArgumentException;
enum EventUndoMode: string
{
case UNDO = 'undo';
case REVERT = 'revert';
public function toExtraInt(): int
{
return match ($this) {
self::UNDO => 1,
self::REVERT => 2,
};
}
public static function fromExtraInt(int $int): self
{
return match ($int) {
1 => self::UNDO,
2 => self::REVERT,
default => throw new InvalidArgumentException('Invalid int ' . (string) $int . ' for EventUndoMode'),
};
}
}

View file

@ -127,9 +127,9 @@ class LogEntryExtraFormatter
}
if (($context instanceof LogWithEventUndoInterface) && $context->isUndoEvent()) {
if ('undo' === $context->getUndoMode()) {
if (EventUndoMode::UNDO === $context->getUndoMode()) {
$array['log.undo_mode.undo'] = '#' . $context->getUndoEventID();
} elseif ('revert' === $context->getUndoMode()) {
} elseif (EventUndoMode::REVERT === $context->getUndoMode()) {
$array['log.undo_mode.revert'] = '#' . $context->getUndoEventID();
}
}