From cc1829f9ed45d38c563f965d05c4ce95239c01ac Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 4 Jul 2016 09:15:50 +0200 Subject: [PATCH] Added pagination to ShortUrls list --- src/Entity/ShortUrl.php | 2 +- .../Rest/ListShortcodesMiddleware.php | 8 ++- .../Adapter/PaginableRepositoryAdapter.php | 56 +++++++++++++++++++ ...y.php => PaginableRepositoryInterface.php} | 12 +++- src/Repository/ShortUrlRepository.php | 20 +++++++ .../ShortUrlRepositoryInterface.php | 2 +- src/Service/ShortUrlService.php | 13 ++++- src/Service/ShortUrlServiceInterface.php | 5 +- 8 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 src/Paginator/Adapter/PaginableRepositoryAdapter.php rename src/Repository/{PaginableRepository.php => PaginableRepositoryInterface.php} (51%) diff --git a/src/Entity/ShortUrl.php b/src/Entity/ShortUrl.php index fe96d651..9f63af68 100644 --- a/src/Entity/ShortUrl.php +++ b/src/Entity/ShortUrl.php @@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM; * @author * @link * - * @ORM\Entity + * @ORM\Entity(repositoryClass="Acelaya\UrlShortener\Repository\ShortUrlRepository") * @ORM\Table(name="short_urls") */ class ShortUrl extends AbstractEntity implements \JsonSerializable diff --git a/src/Middleware/Rest/ListShortcodesMiddleware.php b/src/Middleware/Rest/ListShortcodesMiddleware.php index d437b742..5f3b92a0 100644 --- a/src/Middleware/Rest/ListShortcodesMiddleware.php +++ b/src/Middleware/Rest/ListShortcodesMiddleware.php @@ -8,6 +8,7 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Zend\Diactoros\Response\JsonResponse; +use Zend\Stdlib\ArrayUtils; use Zend\Stratigility\MiddlewareInterface; class ListShortcodesMiddleware implements MiddlewareInterface @@ -60,8 +61,11 @@ class ListShortcodesMiddleware implements MiddlewareInterface return new JsonResponse([ 'shortUrls' => [ - 'data' => $shortUrls, -// 'pagination' => [], + 'data' => ArrayUtils::iteratorToArray($shortUrls->getCurrentItems()), + 'pagination' => [ + 'currentPage' => $shortUrls->getCurrentPageNumber(), + 'pagesCount' => $shortUrls->count(), + ], ] ]); } catch (\Exception $e) { diff --git a/src/Paginator/Adapter/PaginableRepositoryAdapter.php b/src/Paginator/Adapter/PaginableRepositoryAdapter.php new file mode 100644 index 00000000..243e4a1b --- /dev/null +++ b/src/Paginator/Adapter/PaginableRepositoryAdapter.php @@ -0,0 +1,56 @@ +paginableRepository = $paginableRepository; + $this->searchTerm = $searchTerm; + $this->orderBy = $orderBy; + } + + /** + * Returns a collection of items for a page. + * + * @param int $offset Page offset + * @param int $itemCountPerPage Number of items per page + * @return array + */ + public function getItems($offset, $itemCountPerPage) + { + return $this->paginableRepository->findList($itemCountPerPage, $offset, $this->searchTerm, $this->orderBy); + } + + /** + * Count elements of an object + * @link http://php.net/manual/en/countable.count.php + * @return int The custom count as an integer. + *

+ *

+ * 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); }