<?php

namespace App\Repository;

use App\Entity\LockEntites;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
use Faker\Provider\DateTime;

/**
 * @extends ServiceEntityRepository<LockEntites>
 *
 * @method LockEntites|null find($id, $lockMode = null, $lockVersion = null)
 * @method LockEntites|null findOneBy(array $criteria, array $orderBy = null)
 * @method LockEntites[]    findAll()
 * @method LockEntites[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class LockEntitesRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, LockEntites::class);
    }

    /**
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function add(LockEntites $entity, bool $flush = true): void
    {
        $this->_em->persist($entity);
        if ($flush) {
            $this->_em->flush();
        }
    }

    /**
     * @throws ORMException
     * @throws OptimisticLockException
     */
    public function remove(LockEntites $entity, bool $flush = true): void
    {
        $this->_em->remove($entity);
        if ($flush) {
            $this->_em->flush();
        }
    }

    public function getOldLocks() {
        $date = new \DateTime();
        $date->setTimeZone(new \DateTimeZone('Europe/Paris'));
        $date->modify('- 1 hour');
        return $this->createQueryBuilder('l')
            ->andWhere('l.date_debut < :date')
            ->setParameter('date', $date)
            ->getQuery()
            ->getResult()
            ;
    }
}
