diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 552d5e2a3..67b6bff6b 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -64,6 +64,7 @@ ], Tag\TagService::class => ConfigAbstractFactory::class, + Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class], Domain\DomainService::class => ConfigAbstractFactory::class, @@ -153,7 +154,7 @@ Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitIterationRepository::class], Visit\Geolocation\VisitToLocationHelper::class => [IpLocationResolverInterface::class], Visit\VisitsStatsHelper::class => ['em'], - Tag\TagService::class => ['em'], + Tag\TagService::class => ['em', Tag\Repository\TagRepository::class], ShortUrl\DeleteShortUrlService::class => [ 'em', Config\Options\DeleteShortUrlsOptions::class, diff --git a/module/Core/src/Tag/Repository/TagRepositoryInterface.php b/module/Core/src/Tag/Repository/TagRepositoryInterface.php index 236beb14d..b0601b3b8 100644 --- a/module/Core/src/Tag/Repository/TagRepositoryInterface.php +++ b/module/Core/src/Tag/Repository/TagRepositoryInterface.php @@ -4,15 +4,15 @@ namespace Shlinkio\Shlink\Core\Tag\Repository; -use Doctrine\Persistence\ObjectRepository; use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryInterface; +use Shlinkio\Shlink\Core\Repository\EntityRepositoryInterface; use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Model\TagsListFiltering; use Shlinkio\Shlink\Rest\Entity\ApiKey; -/** @extends ObjectRepository */ -interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface +/** @extends EntityRepositoryInterface */ +interface TagRepositoryInterface extends EntityRepositoryInterface, EntitySpecificationRepositoryInterface { public function deleteByName(array $names): int; diff --git a/module/Core/src/Tag/TagService.php b/module/Core/src/Tag/TagService.php index f91c018fb..3681d454e 100644 --- a/module/Core/src/Tag/TagService.php +++ b/module/Core/src/Tag/TagService.php @@ -15,13 +15,12 @@ use Shlinkio\Shlink\Core\Tag\Model\TagsParams; use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsInfoPaginatorAdapter; use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsPaginatorAdapter; -use Shlinkio\Shlink\Core\Tag\Repository\TagRepository; use Shlinkio\Shlink\Core\Tag\Repository\TagRepositoryInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; readonly class TagService implements TagServiceInterface { - public function __construct(private ORM\EntityManagerInterface $em) + public function __construct(private ORM\EntityManagerInterface $em, private TagRepositoryInterface $repo) { } @@ -30,9 +29,7 @@ public function __construct(private ORM\EntityManagerInterface $em) */ public function listTags(TagsParams $params, ApiKey|null $apiKey = null): Paginator { - /** @var TagRepository $repo */ - $repo = $this->em->getRepository(Tag::class); - return $this->createPaginator(new TagsPaginatorAdapter($repo, $params, $apiKey), $params); + return $this->createPaginator(new TagsPaginatorAdapter($this->repo, $params, $apiKey), $params); } /** @@ -40,9 +37,7 @@ public function listTags(TagsParams $params, ApiKey|null $apiKey = null): Pagina */ public function tagsInfo(TagsParams $params, ApiKey|null $apiKey = null): Paginator { - /** @var TagRepositoryInterface $repo */ - $repo = $this->em->getRepository(Tag::class); - return $this->createPaginator(new TagsInfoPaginatorAdapter($repo, $params, $apiKey), $params); + return $this->createPaginator(new TagsInfoPaginatorAdapter($this->repo, $params, $apiKey), $params); } /** @@ -66,9 +61,7 @@ public function deleteTags(array $tagNames, ApiKey|null $apiKey = null): void throw ForbiddenTagOperationException::forDeletion(); } - /** @var TagRepository $repo */ - $repo = $this->em->getRepository(Tag::class); - $repo->deleteByName($tagNames); + $this->repo->deleteByName($tagNames); } /** @@ -80,16 +73,12 @@ public function renameTag(Renaming $renaming, ApiKey|null $apiKey = null): Tag throw ForbiddenTagOperationException::forRenaming(); } - /** @var TagRepository $repo */ - $repo = $this->em->getRepository(Tag::class); - - /** @var Tag|null $tag */ - $tag = $repo->findOneBy(['name' => $renaming->oldName]); + $tag = $this->repo->findOneBy(['name' => $renaming->oldName]); if ($tag === null) { throw TagNotFoundException::fromTag($renaming->oldName); } - $newNameExists = $renaming->nameChanged() && $repo->count(['name' => $renaming->newName]) > 0; + $newNameExists = $renaming->nameChanged() && $this->repo->count(['name' => $renaming->newName]) > 0; if ($newNameExists) { throw TagConflictException::forExistingTag($renaming); } diff --git a/module/Core/test/Tag/TagServiceTest.php b/module/Core/test/Tag/TagServiceTest.php index c1fa8ee76..4080986fd 100644 --- a/module/Core/test/Tag/TagServiceTest.php +++ b/module/Core/test/Tag/TagServiceTest.php @@ -35,9 +35,8 @@ protected function setUp(): void { $this->em = $this->createMock(EntityManagerInterface::class); $this->repo = $this->createMock(TagRepository::class); - $this->em->method('getRepository')->with(Tag::class)->willReturn($this->repo); - $this->service = new TagService($this->em); + $this->service = new TagService($this->em, $this->repo); } #[Test] @@ -166,7 +165,7 @@ public function renameTagToAnExistingNameThrowsException(ApiKey|null $apiKey): v #[Test] public function renamingTagThrowsExceptionWhenProvidedApiKeyIsNotAdmin(): void { - $this->em->expects($this->never())->method('getRepository')->with(Tag::class); + $this->repo->expects($this->never())->method('findOneBy'); $this->expectExceptionMessage(ForbiddenTagOperationException::class); $this->expectExceptionMessage('You are not allowed to rename tags');