<?php
declare(strict_types=1);

namespace App\Doctrine\Core;

use App\Doctrine\AbstractExtension;
use App\Entity\Access\Access;
use App\Entity\Core\NotificationUser;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\SecurityBundle\Security;

/**
 * Filtre de sécurité par défaut pour une resource NotificationUser
 *
 * @package App\Doctrine\Core
 */
final class CurrentNotificationUserExtension extends AbstractExtension
{
    public function __construct(private Security $security)
    { }

    /**
     * @todo : A la suite de la migration, il faut supprimer le where avec le discr.
     */
    protected function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
    {
        if (NotificationUser::class !== $resourceClass) {
            return;
        }

        /** @var Access $currentUser */
        $currentUser = $this->security->getUser();
        $rootAlias = $queryBuilder->getRootAliases()[0];
        $queryBuilder
            ->andWhere(sprintf('%s.access = :current_access', $rootAlias))
            ->setParameter('current_access', $currentUser)
        ;
    }
}
