From 848d574f68da0292602fb58ea7a7d8f2d65bafac Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 11 Aug 2019 13:33:42 +0200 Subject: [PATCH] Moved too concrete class from Common to Core --- .../Command/ShortUrl/ListShortUrlsCommand.php | 4 +- module/Common/src/Entity/AbstractEntity.php | 9 +---- .../Paginator/Util/PaginatorUtilsTrait.php | 6 --- .../PaginableRepositoryInterface.php | 34 ----------------- .../PaginableRepositoryAdapterTest.php | 37 ------------------- .../Adapter/ShortUrlRepositoryAdapter.php} | 18 ++++----- .../ShortUrlRepositoryInterface.php | 21 ++++++++++- module/Core/src/Service/ShortUrlService.php | 6 +-- .../Adapter/ShortUrlRepositoryAdapterTest.php | 37 +++++++++++++++++++ 9 files changed, 71 insertions(+), 101 deletions(-) delete mode 100644 module/Common/src/Repository/PaginableRepositoryInterface.php delete mode 100644 module/Common/test/Paginator/Adapter/PaginableRepositoryAdapterTest.php rename module/{Common/src/Paginator/Adapter/PaginableRepositoryAdapter.php => Core/src/Paginator/Adapter/ShortUrlRepositoryAdapter.php} (71%) create mode 100644 module/Core/test/Paginator/Adapter/ShortUrlRepositoryAdapterTest.php diff --git a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php index 329676c5..ba602fbc 100644 --- a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php +++ b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php @@ -5,9 +5,9 @@ namespace Shlinkio\Shlink\CLI\Command\ShortUrl; use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\CLI\Util\ShlinkTable; -use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter; use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; +use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Symfony\Component\Console\Command\Command; @@ -62,7 +62,7 @@ class ListShortUrlsCommand extends Command 'page', 'p', InputOption::VALUE_OPTIONAL, - sprintf('The first page to list (%s items per page)', PaginableRepositoryAdapter::ITEMS_PER_PAGE), + sprintf('The first page to list (%s items per page)', ShortUrlRepositoryAdapter::ITEMS_PER_PAGE), '1' ) ->addOption( diff --git a/module/Common/src/Entity/AbstractEntity.php b/module/Common/src/Entity/AbstractEntity.php index 9358d2c5..dc3b84bc 100644 --- a/module/Common/src/Entity/AbstractEntity.php +++ b/module/Common/src/Entity/AbstractEntity.php @@ -3,16 +3,9 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Common\Entity; -use Doctrine\ORM\Mapping as ORM; - abstract class AbstractEntity { - /** - * @var string - * @ORM\Id - * @ORM\GeneratedValue(strategy="IDENTITY") - * @ORM\Column(name="id", type="bigint", options={"unsigned"=true}) - */ + /** @var string */ protected $id; public function getId(): string diff --git a/module/Common/src/Paginator/Util/PaginatorUtilsTrait.php b/module/Common/src/Paginator/Util/PaginatorUtilsTrait.php index 7157a21d..2009164d 100644 --- a/module/Common/src/Paginator/Util/PaginatorUtilsTrait.php +++ b/module/Common/src/Paginator/Util/PaginatorUtilsTrait.php @@ -31,12 +31,6 @@ trait PaginatorUtilsTrait return $transformer === null ? $items : array_map([$transformer, 'transform'], $items); } - /** - * Checks if provided paginator is in last page - * - * @param Paginator $paginator - * @return bool - */ private function isLastPage(Paginator $paginator): bool { return $paginator->getCurrentPageNumber() >= $paginator->count(); diff --git a/module/Common/src/Repository/PaginableRepositoryInterface.php b/module/Common/src/Repository/PaginableRepositoryInterface.php deleted file mode 100644 index 7de324e9..00000000 --- a/module/Common/src/Repository/PaginableRepositoryInterface.php +++ /dev/null @@ -1,34 +0,0 @@ -repo = $this->prophesize(PaginableRepositoryInterface::class); - $this->adapter = new PaginableRepositoryAdapter($this->repo->reveal(), 'search', ['foo', 'bar'], 'order'); - } - - /** @test */ - public function getItemsFallbacksToFindList() - { - $this->repo->findList(10, 5, 'search', ['foo', 'bar'], 'order')->shouldBeCalledOnce(); - $this->adapter->getItems(5, 10); - } - - /** @test */ - public function countFallbacksToCountList() - { - $this->repo->countList('search', ['foo', 'bar'])->shouldBeCalledOnce(); - $this->adapter->count(); - } -} diff --git a/module/Common/src/Paginator/Adapter/PaginableRepositoryAdapter.php b/module/Core/src/Paginator/Adapter/ShortUrlRepositoryAdapter.php similarity index 71% rename from module/Common/src/Paginator/Adapter/PaginableRepositoryAdapter.php rename to module/Core/src/Paginator/Adapter/ShortUrlRepositoryAdapter.php index 32511d52..d93063f0 100644 --- a/module/Common/src/Paginator/Adapter/PaginableRepositoryAdapter.php +++ b/module/Core/src/Paginator/Adapter/ShortUrlRepositoryAdapter.php @@ -1,20 +1,20 @@ paginableRepository = $paginableRepository; + $this->repository = $repository; $this->searchTerm = $searchTerm !== null ? trim(strip_tags($searchTerm)) : null; $this->orderBy = $orderBy; $this->tags = $tags; @@ -43,7 +43,7 @@ class PaginableRepositoryAdapter implements AdapterInterface */ public function getItems($offset, $itemCountPerPage): array { - return $this->paginableRepository->findList( + return $this->repository->findList( $itemCountPerPage, $offset, $this->searchTerm, @@ -63,6 +63,6 @@ class PaginableRepositoryAdapter implements AdapterInterface */ public function count(): int { - return $this->paginableRepository->countList($this->searchTerm, $this->tags); + return $this->repository->countList($this->searchTerm, $this->tags); } } diff --git a/module/Core/src/Repository/ShortUrlRepositoryInterface.php b/module/Core/src/Repository/ShortUrlRepositoryInterface.php index b1edab1b..fcec73e4 100644 --- a/module/Core/src/Repository/ShortUrlRepositoryInterface.php +++ b/module/Core/src/Repository/ShortUrlRepositoryInterface.php @@ -4,10 +4,27 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Repository; use Doctrine\Common\Persistence\ObjectRepository; -use Shlinkio\Shlink\Common\Repository\PaginableRepositoryInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; -interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface +interface ShortUrlRepositoryInterface extends ObjectRepository { + /** + * Gets a list of elements using provided filtering data + * + * @param string|array|null $orderBy + */ + public function findList( + ?int $limit = null, + ?int $offset = null, + ?string $searchTerm = null, + array $tags = [], + $orderBy = null + ): array; + + /** + * Counts the number of elements in a list using provided filtering data + */ + public function countList(?string $searchTerm = null, array $tags = []): int; + public function findOneByShortCode(string $shortCode): ?ShortUrl; } diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index 1b95ea34..09ecdc10 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -4,10 +4,10 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Service; use Doctrine\ORM; -use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Model\ShortUrlMeta; +use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Service\ShortUrl\FindShortCodeTrait; use Shlinkio\Shlink\Core\Util\TagManagerTrait; @@ -35,8 +35,8 @@ class ShortUrlService implements ShortUrlServiceInterface { /** @var ShortUrlRepository $repo */ $repo = $this->em->getRepository(ShortUrl::class); - $paginator = new Paginator(new PaginableRepositoryAdapter($repo, $searchQuery, $tags, $orderBy)); - $paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE) + $paginator = new Paginator(new ShortUrlRepositoryAdapter($repo, $searchQuery, $tags, $orderBy)); + $paginator->setItemCountPerPage(ShortUrlRepositoryAdapter::ITEMS_PER_PAGE) ->setCurrentPageNumber($page); return $paginator; diff --git a/module/Core/test/Paginator/Adapter/ShortUrlRepositoryAdapterTest.php b/module/Core/test/Paginator/Adapter/ShortUrlRepositoryAdapterTest.php new file mode 100644 index 00000000..4236fa69 --- /dev/null +++ b/module/Core/test/Paginator/Adapter/ShortUrlRepositoryAdapterTest.php @@ -0,0 +1,37 @@ +repo = $this->prophesize(ShortUrlRepositoryInterface::class); + $this->adapter = new ShortUrlRepositoryAdapter($this->repo->reveal(), 'search', ['foo', 'bar'], 'order'); + } + + /** @test */ + public function getItemsFallbacksToFindList(): void + { + $this->repo->findList(10, 5, 'search', ['foo', 'bar'], 'order')->shouldBeCalledOnce(); + $this->adapter->getItems(5, 10); + } + + /** @test */ + public function countFallbacksToCountList(): void + { + $this->repo->countList('search', ['foo', 'bar'])->shouldBeCalledOnce(); + $this->adapter->count(); + } +}