<?php

declare(strict_types=1);

namespace App\Module\Directory\Infrastructure\EventListener;

use App\Module\Directory\Domain\Entity\CommercialReport;
use App\Shared\Domain\Contract\UserInterface;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Symfony\Bundle\SecurityBundle\Security;

final readonly class CommercialReportAuthorListener
{
    public function __construct(private Security $security) {}

    public function prePersist(CommercialReport $report, PrePersistEventArgs $args): void
    {
        if (null !== $report->getAuthor()) {
            return;
        }

        $user = $this->security->getUser();

        if ($user instanceof UserInterface) {
            $report->setAuthor($user);
        }
    }
}
