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

@ -87,10 +87,10 @@ use InvalidArgumentException;
#[ORM\Entity]
class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventUndoInterface
{
use LogWithEventUndoTrait;
protected string $typeString = 'collection_element_deleted';
public function __construct(AbstractDBElement $changed_element, string $collection_name, AbstractDBElement $deletedElement)
{
parent::__construct();
@ -218,39 +218,4 @@ class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventU
{
return $this->extra['i'];
}
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ('undo' === $mode) {
$this->extra['um'] = 1;
} elseif ('revert' === $mode) {
$this->extra['um'] = 2;
} else {
throw new InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if (1 === $mode_int) {
return 'undo';
}
return 'revert';
}
}

View file

@ -27,12 +27,15 @@ use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Services\LogSystem\EventUndoMode;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
#[ORM\Entity]
class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentInterface, LogWithEventUndoInterface
{
use LogWithEventUndoTrait;
protected string $typeString = 'element_created';
public function __construct(AbstractDBElement $new_element)
@ -79,39 +82,4 @@ class ElementCreatedLogEntry extends AbstractLogEntry implements LogWithCommentI
return $this;
}
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ('undo' === $mode) {
$this->extra['um'] = 1;
} elseif ('revert' === $mode) {
$this->extra['um'] = 2;
} else {
throw new InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if (1 === $mode_int) {
return 'undo';
}
return 'revert';
}
}

View file

@ -29,6 +29,7 @@ use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeTravelInterface;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Services\LogSystem\EventUndoMode;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
@ -37,6 +38,8 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter
{
protected string $typeString = 'element_deleted';
use LogWithEventUndoTrait;
public function __construct(AbstractDBElement $deleted_element)
{
parent::__construct();
@ -112,39 +115,4 @@ class ElementDeletedLogEntry extends AbstractLogEntry implements TimeTravelInter
return $this;
}
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ('undo' === $mode) {
$this->extra['um'] = 1;
} elseif ('revert' === $mode) {
$this->extra['um'] = 2;
} else {
throw new InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if (1 === $mode_int) {
return 'undo';
}
return 'revert';
}
}

View file

@ -33,6 +33,8 @@ use InvalidArgumentException;
#[ORM\Entity]
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface, LogWithNewDataInterface
{
use LogWithEventUndoTrait;
protected string $typeString = 'element_edited';
public function __construct(AbstractDBElement $changed_element)
@ -139,39 +141,4 @@ class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterf
return $this;
}
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
if ('undo' === $mode) {
$this->extra['um'] = 1;
} elseif ('revert' === $mode) {
$this->extra['um'] = 2;
} else {
throw new InvalidArgumentException('Passed invalid $mode!');
}
return $this;
}
public function getUndoMode(): string
{
$mode_int = $this->extra['um'] ?? 1;
if (1 === $mode_int) {
return 'undo';
}
return 'revert';
}
}

View file

@ -0,0 +1,51 @@
<?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\Entity\LogSystem;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Services\LogSystem\EventUndoMode;
trait LogWithEventUndoTrait
{
public function isUndoEvent(): bool
{
return isset($this->extra['u']);
}
public function getUndoEventID(): ?int
{
return $this->extra['u'] ?? null;
}
public function setUndoneEvent(AbstractLogEntry $event, EventUndoMode $mode = EventUndoMode::UNDO): LogWithEventUndoInterface
{
$this->extra['u'] = $event->getID();
$this->extra['um'] = $mode->toExtraInt();
return $this;
}
public function getUndoMode(): EventUndoMode
{
$mode_int = $this->extra['um'] ?? 1;
return EventUndoMode::fromExtraInt($mode_int);
}
}

View file

@ -28,10 +28,6 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class PartStockChangedLogEntry extends AbstractLogEntry
{
final public const TYPE_ADD = "add";
final public const TYPE_WITHDRAW = "withdraw";
final public const TYPE_MOVE = "move";
protected string $typeString = 'part_stock_changed';
protected const COMMENT_MAX_LENGTH = 300;