Refactor subject conversion in AuditEvent2Trail and enhance SubjectConverterInterface

- Replaced `array_map` with `array_reduce` in `AuditEvent2Trail` for flattening converted subjects.
- Enhanced `SubjectConverterInterface` with a `supportsConvert` method and PHPStan annotations to improve type safety and flexibility.
This commit is contained in:
2026-01-28 16:19:25 +01:00
parent e27876aeeb
commit d6f9aa6b45
2 changed files with 21 additions and 1 deletions

View File

@@ -34,7 +34,13 @@ final readonly class AuditEvent2Trail implements AuditEvent2TrailInterface
$event->description->trans($this->translator)
: $event->description;
$targets = array_map(fn (mixed $subject) => $this->subjectConverterManager->convertEntityToSubjects($subject), $event->subjects);
$targets = array_reduce(
$event->subjects,
function (array $carry, mixed $item): array {
return array_merge($carry, $this->subjectConverterManager->convertEntityToSubjects($item));
},
[]
);
$user = $this->security->getUser();

View File

@@ -11,13 +11,27 @@ declare(strict_types=1);
namespace Chill\MainBundle\Audit;
/**
* @template T
*/
interface SubjectConverterInterface
{
/**
* @param T $subject
*
* @return Subject|list<Subject>
*/
public function convert(mixed $subject): Subject|array;
/**
* Determines whether the given subject is supported for conversion.
*
* @param mixed $subject the subject to check for conversion support
*
* @return bool true if the subject can be converted, false otherwise
*
* @phpstan-assert-if-true T $subject
*/
public function supportsConvert(mixed $subject): bool;
public static function getDefaultPriority(): int;