<?php
declare(strict_types=1);

namespace App\Doctrine\Access\Extensions;

use App\Doctrine\Access\AccessExtensionInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;

class DateTimeConstraintExtension implements AccessExtensionInterface {
    public function __construct(
        private RequestStack $requestStack
    ){
    }
    public function support(string $name): bool
    {
        return
            (
                $this->requestStack->getMainRequest()->isMethod('GET') &&
                $this->requestStack->getMainRequest()->get('_time_constraint', true) == true
            );
    }

    public function addWhere(QueryBuilder $queryBuilder): void
    {
        $rootAlias = $queryBuilder->getRootAliases()[0];
        $queryBuilder->innerJoin(sprintf('%s.organizationFunction', $rootAlias), 'organization_function');
    }
}
