Files
chill-bundles/src/Bundle/ChillMainBundle/Audit/AuditEvent2Trail.php
Julien Fastré 32c5f21438 Refactor subject conversion handling and enhance audit logging
- Replaced `Subject|array` return types with `SubjectBag` in `SubjectConverterInterface` for more robust handling.
- Updated `getSubjectsForEntity` to include an optional `$includeAssociated` parameter for finer control over associated subject resolution.
- Refactored `AuditEvent` to differentiate `mainSubject` from associated subjects, improving clarity in audit logging.
- Introduced database schema changes to add `main_subject` and `subjects` columns in the `chill_main_audit_trail` table.
- Added `SubjectBag` class for grouped subject management and implemented deduplication logic.
- Updated all converters and test cases to use the new `SubjectBag` approach, ensuring compatibility.
- Improved event dispatching in controllers to utilize the updated `AuditEvent` structure and refined metadata handling.
2026-02-13 17:07:30 +01:00

56 lines
1.8 KiB
PHP

<?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 Chill\MainBundle\Entity\AuditTrail;
use Chill\MainBundle\Entity\User;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final readonly class AuditEvent2Trail implements AuditEvent2TrailInterface
{
public function __construct(
private TranslatorInterface $translator,
private SubjectConverterManagerInterface $subjectConverterManager,
private ClockInterface $clock,
private Security $security,
) {}
public function convertToTrail(AuditEvent $event): AuditTrail
{
$description = $event->description instanceof TranslatableInterface ?
$event->description->trans($this->translator)
: $event->description;
$subjectBag = $this->subjectConverterManager->getSubjectsForEntity($event->mainSubject, true);
foreach ($event->subjects as $target) {
$subjectBag->append($this->subjectConverterManager->getSubjectsForEntity($target, false));
}
$user = $this->security->getUser();
return new AuditTrail(
Uuid::uuid7(),
$event->action,
$this->clock->now(),
$user instanceof User ? $user : null,
$description,
$subjectBag->subject->asArray(),
array_map(fn (Subject $subject) => $subject->asArray(), $subjectBag->associatedSubjects),
$event->metadata,
);
}
}