Moved the "ENFORCE_CHANGE_COMMENTS_FOR" type to the HistorySettings class

This commit is contained in:
Jan Böhmer 2024-06-25 22:59:22 +02:00
parent 6df7bc5f2a
commit 5ab6a63492
9 changed files with 147 additions and 16 deletions

View file

@ -0,0 +1,49 @@
<?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\Form\History;
use App\Services\LogSystem\EventCommentType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* The type for the "enforceComments" setting in the HistorySettings.
*/
class EnforceEventCommentTypesType extends AbstractType
{
public function getParent(): string
{
return EnumType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'multiple' => true,
'class' => EventCommentType::class,
'empty_data' => [],
]);
}
}

View file

@ -22,14 +22,16 @@ declare(strict_types=1);
*/
namespace App\Services\LogSystem;
use App\Settings\SystemSettings\HistorySettings;
/**
* This service is used to check if a log change comment is needed for a given operation type.
* It is configured using the "enforce_change_comments_for" config parameter.
* @see \App\Tests\Services\LogSystem\EventCommentNeededHelperTest
*/
class EventCommentNeededHelper
final class EventCommentNeededHelper
{
public function __construct(protected array $enforce_change_comments_for)
public function __construct(private readonly HistorySettings $settings)
{
}
@ -39,6 +41,6 @@ class EventCommentNeededHelper
*/
public function isCommentNeeded(EventCommentType $comment_type): bool
{
return in_array($comment_type, $this->enforce_change_comments_for, true);
return in_array($comment_type, $this->settings->enforceComments, true);
}
}

View file

@ -23,11 +23,14 @@ declare(strict_types=1);
namespace App\Services\LogSystem;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* 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
enum EventCommentType: string implements TranslatableInterface
{
case PART_EDIT = 'part_edit';
case PART_CREATE = 'part_create';
@ -36,4 +39,9 @@ enum EventCommentType: string
case DATASTRUCTURE_EDIT = 'datastructure_edit';
case DATASTRUCTURE_CREATE = 'datastructure_create';
case DATASTRUCTURE_DELETE = 'datastructure_delete';
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return $translator->trans('settings.system.history.enforceComments.type.' . $this->value, locale: $locale);
}
}

View file

@ -23,7 +23,11 @@ declare(strict_types=1);
namespace App\Settings\SystemSettings;
use App\Form\History\EnforceEventCommentTypesType;
use App\Services\LogSystem\EventCommentType;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\EnumType;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Symfony\Component\Translation\TranslatableMessage as TM;
@ -53,4 +57,25 @@ class HistorySettings
envVar: "bool:HISTORY_SAVE_REMOVED_DATA", envVarMode: EnvVarMode::OVERWRITE
)]
public bool $saveRemovedData = true;
/** @var EventCommentType[] */
#[SettingsParameter(
type: ArrayType::class,
label: new TM("settings.system.history.enforceComments"),
description: new TM("settings.system.history.enforceComments.description"),
options: ['type' => EnumType::class, 'nullable' => false, 'options' => ['class' => EventCommentType::class]],
formType: EnforceEventCommentTypesType::class,
envVar: "ENFORCE_CHANGE_COMMENTS_FOR", envVarMode: EnvVarMode::OVERWRITE, envVarMapper: [self::class, 'mapEnforceComments']
)]
public array $enforceComments = [];
public static function mapEnforceComments(string $value): array
{
if (trim($value) === '') {
return [];
}
$explode = explode(',', $value);
return array_map(fn(string $type) => EventCommentType::from($type), $explode);
}
}