mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-09 18:04:33 +02:00
Made the magic strings of EventCommentHelper into an array
This commit is contained in:
parent
a0a7ca3c9c
commit
6df7bc5f2a
6 changed files with 62 additions and 33 deletions
|
@ -25,6 +25,7 @@ namespace App\Form\AdminPages;
|
|||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Entity\ProjectSystem\Project;
|
||||
use App\Entity\UserSystem\Group;
|
||||
use App\Services\LogSystem\EventCommentType;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use App\Entity\Base\AbstractNamedDBElement;
|
||||
use App\Entity\Base\AbstractStructuralDBElement;
|
||||
|
@ -152,7 +153,7 @@ class BaseEntityAdminForm extends AbstractType
|
|||
$builder->add('log_comment', TextType::class, [
|
||||
'label' => 'edit.log_comment',
|
||||
'mapped' => false,
|
||||
'required' => $this->eventCommentNeededHelper->isCommentNeeded($is_new ? 'datastructure_create': 'datastructure_edit'),
|
||||
'required' => $this->eventCommentNeededHelper->isCommentNeeded($is_new ? EventCommentType::DATASTRUCTURE_CREATE: EventCommentType::DATASTRUCTURE_EDIT),
|
||||
'empty_data' => null,
|
||||
]);
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ use App\Form\Type\SIUnitType;
|
|||
use App\Form\Type\StructuralEntityType;
|
||||
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
||||
use App\Services\LogSystem\EventCommentNeededHelper;
|
||||
use App\Services\LogSystem\EventCommentType;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
|
@ -263,7 +264,7 @@ class PartBaseType extends AbstractType
|
|||
$builder->add('log_comment', TextType::class, [
|
||||
'label' => 'edit.log_comment',
|
||||
'mapped' => false,
|
||||
'required' => $this->event_comment_needed_helper->isCommentNeeded($new_part ? 'part_create' : 'part_edit'),
|
||||
'required' => $this->event_comment_needed_helper->isCommentNeeded($new_part ? EventCommentType::PART_CREATE : EventCommentType::PART_EDIT),
|
||||
'empty_data' => null,
|
||||
]);
|
||||
|
||||
|
|
|
@ -29,30 +29,16 @@ namespace App\Services\LogSystem;
|
|||
*/
|
||||
class EventCommentNeededHelper
|
||||
{
|
||||
final public const VALID_OPERATION_TYPES = [
|
||||
'part_edit',
|
||||
'part_create',
|
||||
'part_delete',
|
||||
'part_stock_operation',
|
||||
'datastructure_edit',
|
||||
'datastructure_create',
|
||||
'datastructure_delete',
|
||||
];
|
||||
|
||||
public function __construct(protected array $enforce_change_comments_for)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a log change comment is needed for the given operation type
|
||||
*/
|
||||
public function isCommentNeeded(string $comment_type): bool
|
||||
public function isCommentNeeded(EventCommentType $comment_type): bool
|
||||
{
|
||||
//Check if the comment type is valid
|
||||
if (! in_array($comment_type, self::VALID_OPERATION_TYPES, true)) {
|
||||
throw new \InvalidArgumentException('The comment type "'.$comment_type.'" is not valid!');
|
||||
}
|
||||
|
||||
return in_array($comment_type, $this->enforce_change_comments_for, true);
|
||||
}
|
||||
}
|
||||
|
|
39
src/Services/LogSystem/EventCommentType.php
Normal file
39
src/Services/LogSystem/EventCommentType.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Services\LogSystem;
|
||||
|
||||
/**
|
||||
* This enum represents the different types of event comments that could be required, by the system.
|
||||
* They are almost only useful when working with the EventCommentNeededHelper service.
|
||||
*/
|
||||
enum EventCommentType: string
|
||||
{
|
||||
case PART_EDIT = 'part_edit';
|
||||
case PART_CREATE = 'part_create';
|
||||
case PART_DELETE = 'part_delete';
|
||||
case PART_STOCK_OPERATION = 'part_stock_operation';
|
||||
case DATASTRUCTURE_EDIT = 'datastructure_edit';
|
||||
case DATASTRUCTURE_CREATE = 'datastructure_create';
|
||||
case DATASTRUCTURE_DELETE = 'datastructure_delete';
|
||||
}
|
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||
*/
|
||||
namespace App\Twig;
|
||||
|
||||
use App\Services\LogSystem\EventCommentType;
|
||||
use Twig\TwigFunction;
|
||||
use App\Services\LogSystem\EventCommentNeededHelper;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
|
@ -35,9 +36,16 @@ final class MiscExtension extends AbstractExtension
|
|||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('event_comment_needed',
|
||||
fn(string $operation_type) => $this->eventCommentNeededHelper->isCommentNeeded($operation_type)
|
||||
),
|
||||
new TwigFunction('event_comment_needed', $this->evenCommentNeeded(...)),
|
||||
];
|
||||
}
|
||||
|
||||
private function evenCommentNeeded(string|EventCommentType $operation_type): bool
|
||||
{
|
||||
if (is_string($operation_type)) {
|
||||
$operation_type = EventCommentType::from($operation_type);
|
||||
}
|
||||
|
||||
return $this->eventCommentNeededHelper->isCommentNeeded($operation_type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,23 +23,17 @@ declare(strict_types=1);
|
|||
namespace App\Tests\Services\LogSystem;
|
||||
|
||||
use App\Services\LogSystem\EventCommentNeededHelper;
|
||||
use App\Services\LogSystem\EventCommentType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EventCommentNeededHelperTest extends TestCase
|
||||
{
|
||||
public function testIsCommentNeeded(): void
|
||||
{
|
||||
$service = new EventCommentNeededHelper(['part_edit', 'part_create']);
|
||||
$this->assertTrue($service->isCommentNeeded('part_edit'));
|
||||
$this->assertTrue($service->isCommentNeeded('part_create'));
|
||||
$this->assertFalse($service->isCommentNeeded('part_delete'));
|
||||
$this->assertFalse($service->isCommentNeeded('part_stock_operation'));
|
||||
}
|
||||
|
||||
public function testIsCommentNeededInvalidTypeException(): void
|
||||
{
|
||||
$service = new EventCommentNeededHelper(['part_edit', 'part_create']);
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$service->isCommentNeeded('this_is_not_valid');
|
||||
$service = new EventCommentNeededHelper([EventCommentType::PART_CREATE, EventCommentType::PART_EDIT]);
|
||||
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_CREATE));
|
||||
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_EDIT));
|
||||
$this->assertFalse($service->isCommentNeeded(EventCommentType::DATASTRUCTURE_EDIT));
|
||||
$this->assertFalse($service->isCommentNeeded(EventCommentType::PART_STOCK_OPERATION));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue