diff --git a/src/DataTables/Column/EnumColumn.php b/src/DataTables/Column/EnumColumn.php index 1f591108..04813db9 100644 --- a/src/DataTables/Column/EnumColumn.php +++ b/src/DataTables/Column/EnumColumn.php @@ -43,7 +43,7 @@ class EnumColumn extends AbstractColumn return ($this->getEnumClass())::from($value); } - protected function configureOptions(OptionsResolver $resolver) + protected function configureOptions(OptionsResolver $resolver): static { parent::configureOptions($resolver); diff --git a/src/DataTables/LogDataTable.php b/src/DataTables/LogDataTable.php index 87317f40..d36f819a 100644 --- a/src/DataTables/LogDataTable.php +++ b/src/DataTables/LogDataTable.php @@ -139,7 +139,7 @@ class LogDataTable implements DataTableTypeInterface if ($context instanceof PartStockChangedLogEntry) { $text .= sprintf( ' (%s)', - $this->translator->trans('log.part_stock_changed.' . $context->getInstockChangeType()) + $this->translator->trans('log.part_stock_changed.' . $context->getInstockChangeType()->toExtraShortType()) ); } diff --git a/src/Entity/LogSystem/PartStockChangeType.php b/src/Entity/LogSystem/PartStockChangeType.php new file mode 100644 index 00000000..bbd574a7 --- /dev/null +++ b/src/Entity/LogSystem/PartStockChangeType.php @@ -0,0 +1,51 @@ +. + */ + +namespace App\Entity\LogSystem; + +enum PartStockChangeType: string +{ + case ADD = "add"; + case WITHDRAW = "withdraw"; + case MOVE = "move"; + + /** + * Converts the type to a short representation usable in the extra field of the log entry. + * @return string + */ + public function toExtraShortType(): string + { + return match ($this) { + self::ADD => 'a', + self::WITHDRAW => 'w', + self::MOVE => 'm', + }; + } + + public static function fromExtraShortType(string $value): self + { + return match ($value) { + 'a' => self::ADD, + 'w' => self::WITHDRAW, + 'm' => self::MOVE, + default => throw new \InvalidArgumentException("Invalid short type: $value"), + }; + } +} \ No newline at end of file diff --git a/src/Entity/LogSystem/PartStockChangedLogEntry.php b/src/Entity/LogSystem/PartStockChangedLogEntry.php index 34ae15fb..6c56bb48 100644 --- a/src/Entity/LogSystem/PartStockChangedLogEntry.php +++ b/src/Entity/LogSystem/PartStockChangedLogEntry.php @@ -38,7 +38,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry /** * Creates a new part stock changed log entry. - * @param string $type The type of the log entry. One of the TYPE_* constants. + * @param PartStockChangeType $type The type of the log entry. * @param PartLot $lot The part lot which has been changed. * @param float $old_stock The old stock of the lot. * @param float $new_stock The new stock of the lot. @@ -46,14 +46,10 @@ class PartStockChangedLogEntry extends AbstractLogEntry * @param string $comment The comment associated with the change. * @param PartLot|null $move_to_target The target lot if the type is TYPE_MOVE. */ - protected function __construct(string $type, PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, ?PartLot $move_to_target = null) + protected function __construct(PartStockChangeType $type, PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, ?PartLot $move_to_target = null) { parent::__construct(); - if (!in_array($type, [self::TYPE_ADD, self::TYPE_WITHDRAW, self::TYPE_MOVE], true)) { - throw new \InvalidArgumentException('Invalid type for PartStockChangedLogEntry!'); - } - //Same as every other element change log entry $this->level = LogLevel::INFO; @@ -61,7 +57,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry $this->typeString = 'part_stock_changed'; $this->extra = array_merge($this->extra, [ - 't' => $this->typeToShortType($type), + 't' => $type->toExtraShortType(), 'o' => $old_stock, 'n' => $new_stock, 'p' => $new_total_part_instock, @@ -71,7 +67,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry } if ($move_to_target instanceof PartLot) { - if ($type !== self::TYPE_MOVE) { + if ($type !== PartStockChangeType::MOVE) { throw new \InvalidArgumentException('The move_to_target parameter can only be set if the type is "move"!'); } @@ -90,7 +86,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry */ public static function add(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self { - return new self(self::TYPE_ADD, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment); + return new self(PartStockChangeType::ADD, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment); } /** @@ -104,7 +100,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry */ public static function withdraw(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self { - return new self(self::TYPE_WITHDRAW, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment); + return new self(PartStockChangeType::WITHDRAW, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment); } /** @@ -118,16 +114,16 @@ class PartStockChangedLogEntry extends AbstractLogEntry */ public static function move(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, PartLot $move_to_target): self { - return new self(self::TYPE_MOVE, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment, $move_to_target); + return new self(PartStockChangeType::MOVE, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment, $move_to_target); } /** * Returns the instock change type of this entry - * @return string One of the TYPE_* constants. + * @return PartStockChangeType */ - public function getInstockChangeType(): string + public function getInstockChangeType(): PartStockChangeType { - return $this->shortTypeToType($this->extra['t']); + return PartStockChangeType::fromExtraShortType($this->extra['t']); } /** @@ -177,30 +173,4 @@ class PartStockChangedLogEntry extends AbstractLogEntry { return $this->extra['m'] ?? null; } - - /** - * Converts the human-readable type (TYPE_* consts) to the version stored in DB - */ - protected function typeToShortType(string $type): string - { - return match ($type) { - self::TYPE_ADD => 'a', - self::TYPE_WITHDRAW => 'w', - self::TYPE_MOVE => 'm', - default => throw new \InvalidArgumentException('Invalid type: '.$type), - }; - } - - /** - * Converts the short type stored in DB to the human-readable type (TYPE_* consts). - */ - protected function shortTypeToType(string $short_type): string - { - return match ($short_type) { - 'a' => self::TYPE_ADD, - 'w' => self::TYPE_WITHDRAW, - 'm' => self::TYPE_MOVE, - default => throw new \InvalidArgumentException('Invalid short type: '.$short_type), - }; - } } diff --git a/src/Services/LogSystem/LogEntryExtraFormatter.php b/src/Services/LogSystem/LogEntryExtraFormatter.php index 6ace35e8..5852265f 100644 --- a/src/Services/LogSystem/LogEntryExtraFormatter.php +++ b/src/Services/LogSystem/LogEntryExtraFormatter.php @@ -33,6 +33,7 @@ use App\Entity\LogSystem\ElementEditedLogEntry; use App\Entity\LogSystem\ExceptionLogEntry; use App\Entity\LogSystem\LegacyInstockChangedLogEntry; use App\Entity\LogSystem\PartStockChangedLogEntry; +use App\Entity\LogSystem\PartStockChangeType; use App\Entity\LogSystem\SecurityEventLogEntry; use App\Entity\LogSystem\UserLoginLogEntry; use App\Entity\LogSystem\UserLogoutLogEntry; @@ -187,7 +188,7 @@ class LogEntryExtraFormatter if ($context->getComment() !== '') { $array['log.part_stock_changed.comment'] = htmlspecialchars($context->getComment()); } - if ($context->getInstockChangeType() === PartStockChangedLogEntry::TYPE_MOVE) { + if ($context->getInstockChangeType() === PartStockChangeType::MOVE) { $array['log.part_stock_changed.move_target'] = htmlspecialchars($this->elementTypeNameGenerator->getLocalizedTypeLabel(PartLot::class)) .' ' . $context->getMoveToTargetID();