74 lines
1.9 KiB
PHP
74 lines
1.9 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\FoodOrder;
|
|
use DateInterval;
|
|
use DateTimeImmutable;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<FoodOrder>
|
|
*/
|
|
final class FoodOrderRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, FoodOrder::class);
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$this->getEntityManager()
|
|
->flush();
|
|
}
|
|
|
|
/**
|
|
* @return FoodOrder[]
|
|
*/
|
|
public function findLatestEntries(int $page = 1, int $pagesize = 10, int $days = 4): array
|
|
{
|
|
|
|
$result = $this->createQueryBuilder('alias')
|
|
->orderBy('alias.id', 'DESC')
|
|
->setFirstResult(($page - 1) * $pagesize)
|
|
->setMaxResults($pagesize)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
if ($days < 1) {
|
|
return $result;
|
|
}
|
|
|
|
$date = (new DateTimeImmutable)->sub(new DateInterval('P' . $days . 'D'));
|
|
return (new ArrayCollection($result))
|
|
->filter(static fn(FoodOrder $order): bool => $order->getCreatedAt() >= $date)
|
|
->getValues();
|
|
}
|
|
|
|
/**
|
|
* @return FoodOrder[]
|
|
*/
|
|
public function findOpenOrders(): array
|
|
{
|
|
$now = new DateTimeImmutable;
|
|
|
|
return $this->createQueryBuilder('o')
|
|
->where('o.closedAt IS NULL OR o.closedAt > :now')
|
|
->setParameter('now', $now)
|
|
->orderBy('o.id', 'DESC')
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
public function findLatestOrder(): FoodOrder|null
|
|
{
|
|
return $this->createQueryBuilder('alias')
|
|
->orderBy('alias.id', 'DESC')
|
|
->setMaxResults(1)
|
|
->getQuery()
|
|
->getOneOrNullResult();
|
|
}
|
|
}
|