Add TriggerAuditInterface and TriggerAuditService to handle audit event triggering

- Introduced `TriggerAuditInterface` to define the contract for triggering audit events.
- Implemented `TriggerAuditService` to encapsulate audit event creation and persistence logic.
- Added methods to handle audit actions with metadata and translatable descriptions.
This commit is contained in:
2026-02-26 14:13:44 +01:00
parent 1099302ab2
commit 8c88fca2ee
2 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Audit;
use Symfony\Contracts\Translation\TranslatableInterface;
interface TriggerAuditInterface
{
/**
* Handles the triggering of an audit event with the provided details.
*
* @param string $action the action to be performed
* @param object $mainSubject the primary subject on which the action is performed
* @param array $subjects an array of additional subjects related to the action
* @param string|TranslatableInterface $description a description of the action, which can be a string or a translatable interface
* @param array $metadata optional metadata associated with the action
*/
public function triggerAudit(string $action, object $mainSubject, array $subjects = [], string|TranslatableInterface $description = '', array $metadata = []): void;
/**
* Handles the invocation of an action with the provided main subject and additional subjects.
*
* @param string $action the action to be performed
* @param object $mainSubject the primary subject on which the action is performed
* @param array $subjects an array of additional subjects related to the action
* @param string|TranslatableInterface $description a description of the action, which can be a string or a translatable interface
* @param array $metadata optional metadata associated with the action
*/
public function __invoke(string $action, object $mainSubject, array $subjects = [], string|TranslatableInterface $description = '', array $metadata = []): void;
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Audit;
use Symfony\Contracts\Translation\TranslatableInterface;
final readonly class TriggerAuditService implements TriggerAuditInterface
{
public function __construct(
private AuditTrailPersister $auditTrailPersister,
private AuditEvent2TrailInterface $auditEvent2Trail,
) {}
public function triggerAudit(
string $action,
object $mainSubject,
array $subjects = [],
string|TranslatableInterface $description = '',
array $metadata = [],
): void {
$event = new AuditEvent(
$action,
$mainSubject,
$subjects,
$description,
$metadata,
);
$this->auditTrailPersister->persistAuditTrail($this->auditEvent2Trail->convertToTrail($event));
}
public function __invoke(
string $action,
object $mainSubject,
array $subjects = [],
string|TranslatableInterface $description = '',
array $metadata = [],
): void {
$this->triggerAudit($action, $mainSubject, $subjects, $description, $metadata);
}
}