mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-25 08:21:11 -06:00
Inject TagRepository in TagService, instead of getting it from EntityManager
This commit is contained in:
parent
92ad6d2732
commit
dba9302f78
@ -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,
|
||||
|
@ -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<Tag> */
|
||||
interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface
|
||||
/** @extends EntityRepositoryInterface<Tag> */
|
||||
interface TagRepositoryInterface extends EntityRepositoryInterface, EntitySpecificationRepositoryInterface
|
||||
{
|
||||
public function deleteByName(array $names): int;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user