diff --git a/module/Core/src/Repository/TagRepository.php b/module/Core/src/Repository/TagRepository.php index d21122d0..a6871cfc 100644 --- a/module/Core/src/Repository/TagRepository.php +++ b/module/Core/src/Repository/TagRepository.php @@ -49,7 +49,7 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito return map( $query->getResult(), - fn (array $row) => new TagInfo($row['tag'], (int) $row['shortUrlsCount'], (int) $row['visitsCount']), + static fn (array $row) => new TagInfo($row['tag'], (int) $row['shortUrlsCount'], (int) $row['visitsCount']), ); } diff --git a/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php b/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php new file mode 100644 index 00000000..f35e492d --- /dev/null +++ b/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php @@ -0,0 +1,33 @@ +repo->matchSingleScalarResult(Spec::andX( + // FIXME I don't think using Spec::selectNew is the correct thing here, + // but seems to be the only way to use Spec::COUNT + Spec::selectNew(Tag::class, Spec::COUNT('id', true)), + new WithApiKeySpecsEnsuringJoin($this->apiKey), + )); + } +} diff --git a/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php b/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php new file mode 100644 index 00000000..23d7af48 --- /dev/null +++ b/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php @@ -0,0 +1,21 @@ +repo->match(Spec::andX( + new WithApiKeySpecsEnsuringJoin($this->apiKey), + Spec::orderBy('name'), + Spec::limit($length), + Spec::offset($offset), + )); + } +} diff --git a/module/Core/src/Tag/TagService.php b/module/Core/src/Tag/TagService.php index 4da88979..85955b76 100644 --- a/module/Core/src/Tag/TagService.php +++ b/module/Core/src/Tag/TagService.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Tag; use Doctrine\ORM; -use Happyr\DoctrineSpecification\Spec; use Pagerfanta\Adapter\AdapterInterface; use Pagerfanta\Adapter\ArrayAdapter; use Shlinkio\Shlink\Common\Paginator\Paginator; @@ -18,7 +17,7 @@ use Shlinkio\Shlink\Core\Repository\TagRepositoryInterface; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Model\TagRenaming; use Shlinkio\Shlink\Core\Tag\Model\TagsParams; -use Shlinkio\Shlink\Rest\ApiKey\Spec\WithApiKeySpecsEnsuringJoin; +use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsPaginatorAdapter; use Shlinkio\Shlink\Rest\Entity\ApiKey; class TagService implements TagServiceInterface @@ -34,12 +33,7 @@ class TagService implements TagServiceInterface { /** @var TagRepository $repo */ $repo = $this->em->getRepository(Tag::class); - $tags = $repo->match(Spec::andX( - Spec::orderBy('name'), - new WithApiKeySpecsEnsuringJoin($apiKey), - )); - - return $this->createPaginator(new ArrayAdapter($tags), $params); + return $this->createPaginator(new TagsPaginatorAdapter($repo, $params, $apiKey), $params); } /** diff --git a/module/Core/test/Service/Tag/TagServiceTest.php b/module/Core/test/Service/Tag/TagServiceTest.php index b1b1ed5d..d291b3c4 100644 --- a/module/Core/test/Service/Tag/TagServiceTest.php +++ b/module/Core/test/Service/Tag/TagServiceTest.php @@ -47,11 +47,13 @@ class TagServiceTest extends TestCase $expected = [new Tag('foo'), new Tag('bar')]; $match = $this->repo->match(Argument::cetera())->willReturn($expected); + $count = $this->repo->matchSingleScalarResult(Argument::cetera())->willReturn(0); $result = $this->service->listTags(TagsParams::fromRawData([])); self::assertEquals($expected, $result->getCurrentPageResults()); $match->shouldHaveBeenCalled(); + $count->shouldHaveBeenCalled(); } /**