From cc1829f9ed45d38c563f965d05c4ce95239c01ac Mon Sep 17 00:00:00 2001
From: Alejandro Celaya
+ * The return value is cast to an integer. + * @since 5.1.0 + */ + public function count() + { + return $this->paginableRepository->countList($this->searchTerm); + } +} diff --git a/src/Repository/PaginableRepository.php b/src/Repository/PaginableRepositoryInterface.php similarity index 51% rename from src/Repository/PaginableRepository.php rename to src/Repository/PaginableRepositoryInterface.php index ad993961..99c8696e 100644 --- a/src/Repository/PaginableRepository.php +++ b/src/Repository/PaginableRepositoryInterface.php @@ -1,9 +1,11 @@ getQuery()->getResult(); } + + /** + * Counts the number of elements in a list using provided filtering data + * + * @param null $searchTerm + * @return int + */ + public function countList($searchTerm = null) + { + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb->select('COUNT(s)') + ->from(ShortUrl::class, 's'); + + if (isset($searchTerm)) { + // TODO + } + + return (int) $qb->getQuery()->getSingleScalarResult(); + } } diff --git a/src/Repository/ShortUrlRepositoryInterface.php b/src/Repository/ShortUrlRepositoryInterface.php index 37b013d9..be7f3fff 100644 --- a/src/Repository/ShortUrlRepositoryInterface.php +++ b/src/Repository/ShortUrlRepositoryInterface.php @@ -3,6 +3,6 @@ namespace Acelaya\UrlShortener\Repository; use Doctrine\Common\Persistence\ObjectRepository; -interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepository +interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface { } diff --git a/src/Service/ShortUrlService.php b/src/Service/ShortUrlService.php index 2ffc640c..9dbc176b 100644 --- a/src/Service/ShortUrlService.php +++ b/src/Service/ShortUrlService.php @@ -2,6 +2,8 @@ namespace Acelaya\UrlShortener\Service; use Acelaya\UrlShortener\Entity\ShortUrl; +use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter; +use Acelaya\UrlShortener\Repository\ShortUrlRepository; use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Doctrine\ORM\EntityManagerInterface; use Zend\Paginator\Paginator; @@ -25,10 +27,17 @@ class ShortUrlService implements ShortUrlServiceInterface } /** + * @param int $page * @return Paginator|ShortUrl[] */ - public function listShortUrls() + public function listShortUrls($page = 1) { - return $this->em->getRepository(ShortUrl::class)->findAll(); + /** @var ShortUrlRepository $repo */ + $repo = $this->em->getRepository(ShortUrl::class); + $paginator = new Paginator(new PaginableRepositoryAdapter($repo)); + $paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE) + ->setCurrentPageNumber($page); + + return $paginator; } } diff --git a/src/Service/ShortUrlServiceInterface.php b/src/Service/ShortUrlServiceInterface.php index 9f0a219c..a9d182d2 100644 --- a/src/Service/ShortUrlServiceInterface.php +++ b/src/Service/ShortUrlServiceInterface.php @@ -7,7 +7,8 @@ use Zend\Paginator\Paginator; interface ShortUrlServiceInterface { /** - * @return Paginator|ShortUrl[] + * @param int $page + * @return ShortUrl[]|Paginator */ - public function listShortUrls(); + public function listShortUrls($page = 1); }