Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject TagRepository in TagService, instead of getting it from EntityManager #2250

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion module/Core/config/dependencies.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
],

Tag\TagService::class => ConfigAbstractFactory::class,
Tag\Repository\TagRepository::class => [EntityRepositoryFactory::class, Tag\Entity\Tag::class],

Domain\DomainService::class => ConfigAbstractFactory::class,

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions module/Core/src/Tag/Repository/TagRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag> */
interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface
/** @extends EntityRepositoryInterface<Tag> */
interface TagRepositoryInterface extends EntityRepositoryInterface, EntitySpecificationRepositoryInterface
{
public function deleteByName(array $names): int;

Expand Down
23 changes: 6 additions & 17 deletions module/Core/src/Tag/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -30,19 +29,15 @@ 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);
}

/**
* @inheritDoc
*/
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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions module/Core/test/Tag/TagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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');
Expand Down
Loading