From 6df7bc5f2a54a604201b595e50aef9bf08108dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 25 Jun 2024 22:29:04 +0200 Subject: [PATCH] Made the magic strings of EventCommentHelper into an array --- src/Form/AdminPages/BaseEntityAdminForm.php | 3 +- src/Form/Part/PartBaseType.php | 3 +- .../LogSystem/EventCommentNeededHelper.php | 18 +-------- src/Services/LogSystem/EventCommentType.php | 39 +++++++++++++++++++ src/Twig/MiscExtension.php | 14 +++++-- .../EventCommentNeededHelperTest.php | 18 +++------ 6 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 src/Services/LogSystem/EventCommentType.php diff --git a/src/Form/AdminPages/BaseEntityAdminForm.php b/src/Form/AdminPages/BaseEntityAdminForm.php index d1a0ffd0..5a4ef5bc 100644 --- a/src/Form/AdminPages/BaseEntityAdminForm.php +++ b/src/Form/AdminPages/BaseEntityAdminForm.php @@ -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, ]); diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index 4a71f03a..bbd3bdea 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -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, ]); diff --git a/src/Services/LogSystem/EventCommentNeededHelper.php b/src/Services/LogSystem/EventCommentNeededHelper.php index 8440f199..0fffd734 100644 --- a/src/Services/LogSystem/EventCommentNeededHelper.php +++ b/src/Services/LogSystem/EventCommentNeededHelper.php @@ -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); } } diff --git a/src/Services/LogSystem/EventCommentType.php b/src/Services/LogSystem/EventCommentType.php new file mode 100644 index 00000000..ec247e0a --- /dev/null +++ b/src/Services/LogSystem/EventCommentType.php @@ -0,0 +1,39 @@ +. + */ + +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'; +} diff --git a/src/Twig/MiscExtension.php b/src/Twig/MiscExtension.php index 1edef7a3..248d1d28 100644 --- a/src/Twig/MiscExtension.php +++ b/src/Twig/MiscExtension.php @@ -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); + } } diff --git a/tests/Services/LogSystem/EventCommentNeededHelperTest.php b/tests/Services/LogSystem/EventCommentNeededHelperTest.php index 2fb3e123..f365bb63 100644 --- a/tests/Services/LogSystem/EventCommentNeededHelperTest.php +++ b/tests/Services/LogSystem/EventCommentNeededHelperTest.php @@ -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)); } }