diff --git a/src/Controller/LogController.php b/src/Controller/LogController.php index d906d964..afa32f7a 100644 --- a/src/Controller/LogController.php +++ b/src/Controller/LogController.php @@ -34,6 +34,7 @@ use App\Entity\LogSystem\ElementEditedLogEntry; use App\Form\Filters\LogFilterType; use App\Repository\DBElementRepository; use App\Services\LogSystem\EventUndoHelper; +use App\Services\LogSystem\EventUndoMode; use App\Services\LogSystem\LogEntryExtraFormatter; use App\Services\LogSystem\LogLevelHelper; use App\Services\LogSystem\LogTargetHelper; @@ -128,13 +129,17 @@ class LogController extends AbstractController #[Route(path: '/undo', name: 'log_undo', methods: ['POST'])] public function undoRevertLog(Request $request, EventUndoHelper $eventUndoHelper): RedirectResponse { - $mode = EventUndoHelper::MODE_UNDO; - $id = $request->request->get('undo'); + $mode = EventUndoMode::UNDO; + $id = $request->request->getInt('undo'); //If no undo value was set check if a revert was set - if (null === $id) { - $id = $request->get('revert'); - $mode = EventUndoHelper::MODE_REVERT; + if (0 === $id) { + $id = $request->request->getInt('revert'); + $mode = EventUndoMode::REVERT; + } + + if (0 === $id) { + throw new InvalidArgumentException('No log entry ID was given!'); } $log_element = $this->entityManager->find(AbstractLogEntry::class, $id); @@ -147,9 +152,9 @@ class LogController extends AbstractController $eventUndoHelper->setMode($mode); $eventUndoHelper->setUndoneEvent($log_element); - if (EventUndoHelper::MODE_UNDO === $mode) { + if (EventUndoMode::UNDO === $mode) { $this->undoLog($log_element); - } elseif (EventUndoHelper::MODE_REVERT === $mode) { + } elseif (EventUndoMode::REVERT === $mode) { $this->revertLog($log_element); } diff --git a/src/Entity/Base/AbstractStructuralDBElement.php b/src/Entity/Base/AbstractStructuralDBElement.php index a7474330..eee8ebcc 100644 --- a/src/Entity/Base/AbstractStructuralDBElement.php +++ b/src/Entity/Base/AbstractStructuralDBElement.php @@ -67,8 +67,6 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement { use ParametersTrait; - final public const ID_ROOT_ELEMENT = 0; - /** * This is a not standard character, so build a const, so a dev can easily use it. */ diff --git a/src/Entity/Contracts/LogWithEventUndoInterface.php b/src/Entity/Contracts/LogWithEventUndoInterface.php index fecc6eaa..1e8db0a1 100644 --- a/src/Entity/Contracts/LogWithEventUndoInterface.php +++ b/src/Entity/Contracts/LogWithEventUndoInterface.php @@ -42,6 +42,7 @@ declare(strict_types=1); namespace App\Entity\Contracts; use App\Entity\LogSystem\AbstractLogEntry; +use App\Services\LogSystem\EventUndoMode; interface LogWithEventUndoInterface { @@ -60,12 +61,12 @@ interface LogWithEventUndoInterface * * @return $this */ - public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): self; + public function setUndoneEvent(AbstractLogEntry $event, EventUndoMode $mode = EventUndoMode::UNDO): self; /** * Returns the mode how the event was undone: * "undo" = Only a single event was applied to element * "revert" = Element was reverted to the state it was to the timestamp of the log. */ - public function getUndoMode(): string; + public function getUndoMode(): EventUndoMode; } diff --git a/src/Entity/LogSystem/CollectionElementDeleted.php b/src/Entity/LogSystem/CollectionElementDeleted.php index 50eed6ab..c3980a40 100644 --- a/src/Entity/LogSystem/CollectionElementDeleted.php +++ b/src/Entity/LogSystem/CollectionElementDeleted.php @@ -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'; - } } diff --git a/src/Entity/LogSystem/ElementCreatedLogEntry.php b/src/Entity/LogSystem/ElementCreatedLogEntry.php index 90f72d48..7d5968a8 100644 --- a/src/Entity/LogSystem/ElementCreatedLogEntry.php +++ b/src/Entity/LogSystem/ElementCreatedLogEntry.php @@ -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'; - } } diff --git a/src/Entity/LogSystem/ElementDeletedLogEntry.php b/src/Entity/LogSystem/ElementDeletedLogEntry.php index 6b0e0f0a..836e8d60 100644 --- a/src/Entity/LogSystem/ElementDeletedLogEntry.php +++ b/src/Entity/LogSystem/ElementDeletedLogEntry.php @@ -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'; - } } diff --git a/src/Entity/LogSystem/ElementEditedLogEntry.php b/src/Entity/LogSystem/ElementEditedLogEntry.php index d524f3c1..4279ac63 100644 --- a/src/Entity/LogSystem/ElementEditedLogEntry.php +++ b/src/Entity/LogSystem/ElementEditedLogEntry.php @@ -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'; - } } diff --git a/src/Entity/LogSystem/LogWithEventUndoTrait.php b/src/Entity/LogSystem/LogWithEventUndoTrait.php new file mode 100644 index 00000000..16568241 --- /dev/null +++ b/src/Entity/LogSystem/LogWithEventUndoTrait.php @@ -0,0 +1,51 @@ +. + */ + +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); + } +} \ No newline at end of file diff --git a/src/Entity/LogSystem/PartStockChangedLogEntry.php b/src/Entity/LogSystem/PartStockChangedLogEntry.php index 6c56bb48..63288d0d 100644 --- a/src/Entity/LogSystem/PartStockChangedLogEntry.php +++ b/src/Entity/LogSystem/PartStockChangedLogEntry.php @@ -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; diff --git a/src/Services/LogSystem/EventUndoHelper.php b/src/Services/LogSystem/EventUndoHelper.php index 2416ced0..ed3bc363 100644 --- a/src/Services/LogSystem/EventUndoHelper.php +++ b/src/Services/LogSystem/EventUndoHelper.php @@ -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; } diff --git a/src/Services/LogSystem/EventUndoMode.php b/src/Services/LogSystem/EventUndoMode.php new file mode 100644 index 00000000..51ad664e --- /dev/null +++ b/src/Services/LogSystem/EventUndoMode.php @@ -0,0 +1,46 @@ +. + */ + +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'), + }; + } +} diff --git a/src/Services/LogSystem/LogEntryExtraFormatter.php b/src/Services/LogSystem/LogEntryExtraFormatter.php index 5852265f..a1fd4c9b 100644 --- a/src/Services/LogSystem/LogEntryExtraFormatter.php +++ b/src/Services/LogSystem/LogEntryExtraFormatter.php @@ -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(); } } diff --git a/templates/log_system/details/helper.macro.html.twig b/templates/log_system/details/helper.macro.html.twig index d2af5201..1d774910 100644 --- a/templates/log_system/details/helper.macro.html.twig +++ b/templates/log_system/details/helper.macro.html.twig @@ -19,8 +19,8 @@
diff --git a/templates/log_system/details/log_details.html.twig b/templates/log_system/details/log_details.html.twig index 6dedd8ee..92fd65bb 100644 --- a/templates/log_system/details/log_details.html.twig +++ b/templates/log_system/details/log_details.html.twig @@ -26,11 +26,11 @@