mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-03 06:54:34 +02:00
Use an enum for target_type in log entries
This commit is contained in:
parent
2da7463edf
commit
9adfcc7aec
12 changed files with 359 additions and 210 deletions
|
@ -63,50 +63,6 @@ use App\Repository\LogEntryRepository;
|
|||
#[ORM\Index(columns: ['datetime'], name: 'log_idx_datetime')]
|
||||
abstract class AbstractLogEntry extends AbstractDBElement
|
||||
{
|
||||
protected const TARGET_TYPE_NONE = 0;
|
||||
protected const TARGET_TYPE_USER = 1;
|
||||
protected const TARGET_TYPE_ATTACHEMENT = 2;
|
||||
protected const TARGET_TYPE_ATTACHEMENTTYPE = 3;
|
||||
protected const TARGET_TYPE_CATEGORY = 4;
|
||||
protected const TARGET_TYPE_DEVICE = 5;
|
||||
protected const TARGET_TYPE_DEVICEPART = 6;
|
||||
protected const TARGET_TYPE_FOOTPRINT = 7;
|
||||
protected const TARGET_TYPE_GROUP = 8;
|
||||
protected const TARGET_TYPE_MANUFACTURER = 9;
|
||||
protected const TARGET_TYPE_PART = 10;
|
||||
protected const TARGET_TYPE_STORELOCATION = 11;
|
||||
protected const TARGET_TYPE_SUPPLIER = 12;
|
||||
protected const TARGET_TYPE_PARTLOT = 13;
|
||||
protected const TARGET_TYPE_CURRENCY = 14;
|
||||
protected const TARGET_TYPE_ORDERDETAIL = 15;
|
||||
protected const TARGET_TYPE_PRICEDETAIL = 16;
|
||||
protected const TARGET_TYPE_MEASUREMENTUNIT = 17;
|
||||
protected const TARGET_TYPE_PARAMETER = 18;
|
||||
protected const TARGET_TYPE_LABEL_PROFILE = 19;
|
||||
|
||||
|
||||
protected const TARGET_CLASS_MAPPING = [
|
||||
self::TARGET_TYPE_USER => User::class,
|
||||
self::TARGET_TYPE_ATTACHEMENT => Attachment::class,
|
||||
self::TARGET_TYPE_ATTACHEMENTTYPE => AttachmentType::class,
|
||||
self::TARGET_TYPE_CATEGORY => Category::class,
|
||||
self::TARGET_TYPE_DEVICE => Project::class,
|
||||
self::TARGET_TYPE_DEVICEPART => ProjectBOMEntry::class,
|
||||
self::TARGET_TYPE_FOOTPRINT => Footprint::class,
|
||||
self::TARGET_TYPE_GROUP => Group::class,
|
||||
self::TARGET_TYPE_MANUFACTURER => Manufacturer::class,
|
||||
self::TARGET_TYPE_PART => Part::class,
|
||||
self::TARGET_TYPE_STORELOCATION => Storelocation::class,
|
||||
self::TARGET_TYPE_SUPPLIER => Supplier::class,
|
||||
self::TARGET_TYPE_PARTLOT => PartLot::class,
|
||||
self::TARGET_TYPE_CURRENCY => Currency::class,
|
||||
self::TARGET_TYPE_ORDERDETAIL => Orderdetail::class,
|
||||
self::TARGET_TYPE_PRICEDETAIL => Pricedetail::class,
|
||||
self::TARGET_TYPE_MEASUREMENTUNIT => MeasurementUnit::class,
|
||||
self::TARGET_TYPE_PARAMETER => AbstractParameter::class,
|
||||
self::TARGET_TYPE_LABEL_PROFILE => LabelProfile::class,
|
||||
];
|
||||
|
||||
/** @var User|null The user which has caused this log entry
|
||||
*/
|
||||
#[ORM\ManyToOne(targetEntity: User::class, fetch: 'EAGER')]
|
||||
|
@ -135,10 +91,10 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
|||
#[ORM\Column(name: 'target_id', type: Types::INTEGER)]
|
||||
protected int $target_id = 0;
|
||||
|
||||
/** @var int The Type of the targeted element
|
||||
/** @var LogTargetType The Type of the targeted element
|
||||
*/
|
||||
#[ORM\Column(name: 'target_type', type: Types::SMALLINT)]
|
||||
protected int $target_type = 0;
|
||||
#[ORM\Column(name: 'target_type', type: Types::SMALLINT, enumType: LogTargetType::class)]
|
||||
protected LogTargetType $target_type = LogTargetType::NONE;
|
||||
|
||||
/** @var string The type of this log entry, aka the description what has happened.
|
||||
* The mapping between the log entry class and the discriminator column is done by doctrine.
|
||||
|
@ -299,11 +255,16 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
|||
*/
|
||||
public function getTargetClass(): ?string
|
||||
{
|
||||
if (self::TARGET_TYPE_NONE === $this->target_type) {
|
||||
return null;
|
||||
}
|
||||
return $this->target_type->toClass();
|
||||
}
|
||||
|
||||
return self::targetTypeIdToClass($this->target_type);
|
||||
/**
|
||||
* Returns the type of the target element associated with this log entry.
|
||||
* @return LogTargetType
|
||||
*/
|
||||
public function getTargetType(): LogTargetType
|
||||
{
|
||||
return $this->target_type;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -340,14 +301,14 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
|||
*/
|
||||
public function setTargetElement(?AbstractDBElement $element): self
|
||||
{
|
||||
if (!$element instanceof AbstractDBElement) {
|
||||
if ($element === null) {
|
||||
$this->target_id = 0;
|
||||
$this->target_type = self::TARGET_TYPE_NONE;
|
||||
$this->target_type = LogTargetType::NONE;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->target_type = static::targetTypeClassToID($element::class);
|
||||
$this->target_type = LogTargetType::fromElementClass($element);
|
||||
$this->target_id = $element->getID();
|
||||
|
||||
return $this;
|
||||
|
@ -370,42 +331,4 @@ abstract class AbstractLogEntry extends AbstractDBElement
|
|||
return $this->extra;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a target type id to a full qualified class name.
|
||||
*
|
||||
* @param int $type_id The target type ID
|
||||
*/
|
||||
final public static function targetTypeIdToClass(int $type_id): string
|
||||
{
|
||||
if (!isset(self::TARGET_CLASS_MAPPING[$type_id])) {
|
||||
throw new InvalidArgumentException('No target type with this ID is existing!');
|
||||
}
|
||||
|
||||
return self::TARGET_CLASS_MAPPING[$type_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a class name to a target type ID.
|
||||
*
|
||||
* @param string $class The name of the class (FQN) that should be converted to id
|
||||
*
|
||||
* @return int the ID of the associated target type ID
|
||||
*/
|
||||
final public static function targetTypeClassToID(string $class): int
|
||||
{
|
||||
$tmp = array_flip(self::TARGET_CLASS_MAPPING);
|
||||
//Check if we can use a key directly
|
||||
if (isset($tmp[$class])) {
|
||||
return $tmp[$class];
|
||||
}
|
||||
|
||||
//Otherwise we have to iterate over everything and check for inheritance
|
||||
foreach ($tmp as $compare_class => $class_id) {
|
||||
if (is_a($class, $compare_class, true)) {
|
||||
return $class_id;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('No target ID for this class is existing! (Class: '.$class.')');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,6 +89,8 @@ class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventU
|
|||
{
|
||||
protected string $typeString = 'collection_element_deleted';
|
||||
|
||||
|
||||
|
||||
public function __construct(AbstractDBElement $changed_element, string $collection_name, AbstractDBElement $deletedElement)
|
||||
{
|
||||
parent::__construct();
|
||||
|
@ -97,7 +99,7 @@ class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventU
|
|||
|
||||
$this->setTargetElement($changed_element);
|
||||
$this->extra['n'] = $collection_name;
|
||||
$this->extra['c'] = self::targetTypeClassToID($deletedElement::class);
|
||||
$this->extra['c'] = LogTargetType::fromElementClass($deletedElement)->value;
|
||||
$this->extra['i'] = $deletedElement->getID();
|
||||
if ($deletedElement instanceof NamedElementInterface) {
|
||||
$this->extra['o'] = $deletedElement->getName();
|
||||
|
@ -127,7 +129,7 @@ class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventU
|
|||
public function getDeletedElementClass(): string
|
||||
{
|
||||
//The class name of our target element
|
||||
$tmp = self::targetTypeIdToClass($this->extra['c']);
|
||||
$tmp = LogTargetType::from($this->extra['c'])->toClass();
|
||||
|
||||
$reflection_class = new \ReflectionClass($tmp);
|
||||
//If the class is abstract, we have to map it to an instantiable class
|
||||
|
|
123
src/Entity/LogSystem/LogTargetType.php
Normal file
123
src/Entity/LogSystem/LogTargetType.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?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\Attachments\Attachment;
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\LabelSystem\LabelProfile;
|
||||
use App\Entity\Parameters\AbstractParameter;
|
||||
use App\Entity\Parts\Category;
|
||||
use App\Entity\Parts\Footprint;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use App\Entity\Parts\Storelocation;
|
||||
use App\Entity\Parts\Supplier;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Entity\PriceInformations\Orderdetail;
|
||||
use App\Entity\PriceInformations\Pricedetail;
|
||||
use App\Entity\ProjectSystem\Project;
|
||||
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
||||
use App\Entity\UserSystem\Group;
|
||||
use App\Entity\UserSystem\User;
|
||||
|
||||
enum LogTargetType: int
|
||||
{
|
||||
case NONE = 0;
|
||||
case USER = 1;
|
||||
case ATTACHMENT = 2;
|
||||
case ATTACHMENT_TYPE = 3;
|
||||
case CATEGORY = 4;
|
||||
case PROJECT = 5;
|
||||
case BOM_ENTRY = 6;
|
||||
case FOOTPRINT = 7;
|
||||
case GROUP = 8;
|
||||
case MANUFACTURER = 9;
|
||||
case PART = 10;
|
||||
case STORELOCATION = 11;
|
||||
case SUPPLIER = 12;
|
||||
case PART_LOT = 13;
|
||||
case CURRENCY = 14;
|
||||
case ORDERDETAIL = 15;
|
||||
case PRICEDETAIL = 16;
|
||||
case MEASUREMENT_UNIT = 17;
|
||||
case PARAMETER = 18;
|
||||
case LABEL_PROFILE = 19;
|
||||
|
||||
/**
|
||||
* Returns the class name of the target type or null if the target type is NONE.
|
||||
* @return string|null
|
||||
*/
|
||||
public function toClass(): ?string
|
||||
{
|
||||
return match ($this) {
|
||||
self::NONE => null,
|
||||
self::USER => User::class,
|
||||
self::ATTACHMENT => Attachment::class,
|
||||
self::ATTACHMENT_TYPE => AttachmentType::class,
|
||||
self::CATEGORY => Category::class,
|
||||
self::PROJECT => Project::class,
|
||||
self::BOM_ENTRY => ProjectBOMEntry::class,
|
||||
self::FOOTPRINT => Footprint::class,
|
||||
self::GROUP => Group::class,
|
||||
self::MANUFACTURER => Manufacturer::class,
|
||||
self::PART => Part::class,
|
||||
self::STORELOCATION => Storelocation::class,
|
||||
self::SUPPLIER => Supplier::class,
|
||||
self::PART_LOT => PartLot::class,
|
||||
self::CURRENCY => Currency::class,
|
||||
self::ORDERDETAIL => Orderdetail::class,
|
||||
self::PRICEDETAIL => Pricedetail::class,
|
||||
self::MEASUREMENT_UNIT => MeasurementUnit::class,
|
||||
self::PARAMETER => AbstractParameter::class,
|
||||
self::LABEL_PROFILE => LabelProfile::class,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the target type from the given class name or object.
|
||||
* @param object|string $element
|
||||
* @phpstan-param object|class-string $element
|
||||
* @return self
|
||||
*/
|
||||
public static function fromElementClass(object|string $element): self
|
||||
{
|
||||
//Iterate over all possible types
|
||||
foreach (self::cases() as $case) {
|
||||
$class = $case->toClass();
|
||||
|
||||
//Skip NONE
|
||||
if ($class === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//Check if the given element is a instance of the class
|
||||
if (is_a($element, $class, true)) {
|
||||
return $case;
|
||||
}
|
||||
}
|
||||
|
||||
$elementClass = is_object($element) ? get_class($element) : $element;
|
||||
//If no matching type was found, throw an exception
|
||||
throw new \InvalidArgumentException("The given class $elementClass is not a valid log target type.");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue