Inject TagRepository in TagService, instead of getting it from EntityManager

This commit is contained in:
Alejandro Celaya 2024-11-09 09:25:01 +01:00
parent 92ad6d2732
commit dba9302f78
4 changed files with 13 additions and 24 deletions

View File

@ -64,6 +64,7 @@ return [
], ],
Tag\TagService::class => ConfigAbstractFactory::class, Tag\TagService::class => ConfigAbstractFactory::class,
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],
Domain\DomainService::class => ConfigAbstractFactory::class, Domain\DomainService::class => ConfigAbstractFactory::class,
@ -153,7 +154,7 @@ return [
Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitIterationRepository::class], Visit\Geolocation\VisitLocator::class => ['em', Visit\Repository\VisitIterationRepository::class],
Visit\Geolocation\VisitToLocationHelper::class => [IpLocationResolverInterface::class], Visit\Geolocation\VisitToLocationHelper::class => [IpLocationResolverInterface::class],
Visit\VisitsStatsHelper::class => ['em'], Visit\VisitsStatsHelper::class => ['em'],
Tag\TagService::class => ['em'], Tag\TagService::class => ['em', Tag\Repository\TagRepository::class],
ShortUrl\DeleteShortUrlService::class => [ ShortUrl\DeleteShortUrlService::class => [
'em', 'em',
Config\Options\DeleteShortUrlsOptions::class, Config\Options\DeleteShortUrlsOptions::class,

View File

@ -4,15 +4,15 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Tag\Repository; namespace Shlinkio\Shlink\Core\Tag\Repository;
use Doctrine\Persistence\ObjectRepository;
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryInterface; use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryInterface;
use Shlinkio\Shlink\Core\Repository\EntityRepositoryInterface;
use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
use Shlinkio\Shlink\Core\Tag\Model\TagsListFiltering; use Shlinkio\Shlink\Core\Tag\Model\TagsListFiltering;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
/** @extends ObjectRepository<Tag> */ /** @extends EntityRepositoryInterface<Tag> */
interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface interface TagRepositoryInterface extends EntityRepositoryInterface, EntitySpecificationRepositoryInterface
{ {
public function deleteByName(array $names): int; public function deleteByName(array $names): int;

View File

@ -15,13 +15,12 @@ use Shlinkio\Shlink\Core\Tag\Entity\Tag;
use Shlinkio\Shlink\Core\Tag\Model\TagsParams; use Shlinkio\Shlink\Core\Tag\Model\TagsParams;
use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsInfoPaginatorAdapter; use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsInfoPaginatorAdapter;
use Shlinkio\Shlink\Core\Tag\Paginator\Adapter\TagsPaginatorAdapter; 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\Core\Tag\Repository\TagRepositoryInterface;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
readonly class TagService implements TagServiceInterface 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 public function listTags(TagsParams $params, ApiKey|null $apiKey = null): Paginator
{ {
/** @var TagRepository $repo */ return $this->createPaginator(new TagsPaginatorAdapter($this->repo, $params, $apiKey), $params);
$repo = $this->em->getRepository(Tag::class);
return $this->createPaginator(new TagsPaginatorAdapter($repo, $params, $apiKey), $params);
} }
/** /**
@ -40,9 +37,7 @@ readonly class TagService implements TagServiceInterface
*/ */
public function tagsInfo(TagsParams $params, ApiKey|null $apiKey = null): Paginator public function tagsInfo(TagsParams $params, ApiKey|null $apiKey = null): Paginator
{ {
/** @var TagRepositoryInterface $repo */ return $this->createPaginator(new TagsInfoPaginatorAdapter($this->repo, $params, $apiKey), $params);
$repo = $this->em->getRepository(Tag::class);
return $this->createPaginator(new TagsInfoPaginatorAdapter($repo, $params, $apiKey), $params);
} }
/** /**
@ -66,9 +61,7 @@ readonly class TagService implements TagServiceInterface
throw ForbiddenTagOperationException::forDeletion(); throw ForbiddenTagOperationException::forDeletion();
} }
/** @var TagRepository $repo */ $this->repo->deleteByName($tagNames);
$repo = $this->em->getRepository(Tag::class);
$repo->deleteByName($tagNames);
} }
/** /**
@ -80,16 +73,12 @@ readonly class TagService implements TagServiceInterface
throw ForbiddenTagOperationException::forRenaming(); throw ForbiddenTagOperationException::forRenaming();
} }
/** @var TagRepository $repo */ $tag = $this->repo->findOneBy(['name' => $renaming->oldName]);
$repo = $this->em->getRepository(Tag::class);
/** @var Tag|null $tag */
$tag = $repo->findOneBy(['name' => $renaming->oldName]);
if ($tag === null) { if ($tag === null) {
throw TagNotFoundException::fromTag($renaming->oldName); 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) { if ($newNameExists) {
throw TagConflictException::forExistingTag($renaming); throw TagConflictException::forExistingTag($renaming);
} }

View File

@ -35,9 +35,8 @@ class TagServiceTest extends TestCase
{ {
$this->em = $this->createMock(EntityManagerInterface::class); $this->em = $this->createMock(EntityManagerInterface::class);
$this->repo = $this->createMock(TagRepository::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] #[Test]
@ -166,7 +165,7 @@ class TagServiceTest extends TestCase
#[Test] #[Test]
public function renamingTagThrowsExceptionWhenProvidedApiKeyIsNotAdmin(): void 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(ForbiddenTagOperationException::class);
$this->expectExceptionMessage('You are not allowed to rename tags'); $this->expectExceptionMessage('You are not allowed to rename tags');