From dba9302f78242a735cd8e35f3248c55304fb0edc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Nov 2024 09:25:01 +0100 Subject: [PATCH] Inject TagRepository in TagService, instead of getting it from EntityManager --- module/Core/config/dependencies.config.php | 3 ++- .../Tag/Repository/TagRepositoryInterface.php | 6 ++--- module/Core/src/Tag/TagService.php | 23 +++++-------------- module/Core/test/Tag/TagServiceTest.php | 5 ++-- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 552d5e2a..67b6bff6 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -64,6 +64,7 @@ return [ ], Tag\TagService::class => ConfigAbstractFactory::class, + Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class], Domain\DomainService::class => ConfigAbstractFactory::class, @@ -153,7 +154,7 @@ return [ 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 236beb14..b0601b3b 100644 --- a/module/Core/src/Tag/Repository/TagRepositoryInterface.php +++ b/module/Core/src/Tag/Repository/TagRepositoryInterface.php @@ -4,15 +4,15 @@ declare(strict_types=1); 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 f91c018f..3681d454 100644 --- a/module/Core/src/Tag/TagService.php +++ b/module/Core/src/Tag/TagService.php @@ -15,13 +15,12 @@ use Shlinkio\Shlink\Core\Tag\Entity\Tag; 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 @@ readonly class TagService implements TagServiceInterface */ 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 @@ readonly class TagService implements TagServiceInterface */ 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 @@ readonly class TagService implements TagServiceInterface throw ForbiddenTagOperationException::forDeletion(); } - /** @var TagRepository $repo */ - $repo = $this->em->getRepository(Tag::class); - $repo->deleteByName($tagNames); + $this->repo->deleteByName($tagNames); } /** @@ -80,16 +73,12 @@ readonly class TagService implements TagServiceInterface 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 c1fa8ee7..4080986f 100644 --- a/module/Core/test/Tag/TagServiceTest.php +++ b/module/Core/test/Tag/TagServiceTest.php @@ -35,9 +35,8 @@ class TagServiceTest extends TestCase { $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 @@ class TagServiceTest extends TestCase #[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');