futtern/src/Repository/FoodOrderRepository.php
lubiana 5d41b6fef5
All checks were successful
/ ls (pull_request) Successful in 31s
/ ls (push) Successful in 32s
/ ls (release) Successful in 25s
#33: limit orders on first page and paginate
2024-07-08 21:23:35 +02:00

52 lines
1.4 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(FoodOrder $order): void
{
$this->getEntityManager()
->persist($order);
$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();
}
}