From 532102e66252839b7d8756ab644102ed7964e83c Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 9 Nov 2024 09:39:56 +0100 Subject: [PATCH] Inject ShortUrlRepository in ShortUrlResolver --- module/Core/config/dependencies.config.php | 9 ++++++++- module/Core/src/Importer/ImportedLinksProcessor.php | 10 +++++----- module/Core/src/ShortUrl/ShortUrlResolver.php | 13 ++++--------- module/Core/test/ShortUrl/ShortUrlResolverTest.php | 10 +--------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 9852bdad..4afb28d5 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -50,6 +50,10 @@ return [ ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class, ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class, ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class, + ShortUrl\Repository\ShortUrlRepository::class => [ + EntityRepositoryFactory::class, + ShortUrl\Entity\ShortUrl::class, + ], ShortUrl\Repository\ShortUrlListRepository::class => [ EntityRepositoryFactory::class, ShortUrl\Entity\ShortUrl::class, @@ -162,7 +166,10 @@ return [ ShortUrl\ShortUrlResolver::class, ShortUrl\Repository\ExpiredShortUrlsRepository::class, ], - ShortUrl\ShortUrlResolver::class => ['em', Config\Options\UrlShortenerOptions::class], + ShortUrl\ShortUrlResolver::class => [ + ShortUrl\Repository\ShortUrlRepository::class, + Config\Options\UrlShortenerOptions::class, + ], ShortUrl\ShortUrlVisitsDeleter::class => [ Visit\Repository\VisitDeleterRepository::class, ShortUrl\ShortUrlResolver::class, diff --git a/module/Core/src/Importer/ImportedLinksProcessor.php b/module/Core/src/Importer/ImportedLinksProcessor.php index 16da0a09..266e9a7a 100644 --- a/module/Core/src/Importer/ImportedLinksProcessor.php +++ b/module/Core/src/Importer/ImportedLinksProcessor.php @@ -25,13 +25,13 @@ use Throwable; use function Shlinkio\Shlink\Core\normalizeDate; use function sprintf; -class ImportedLinksProcessor implements ImportedLinksProcessorInterface +readonly class ImportedLinksProcessor implements ImportedLinksProcessorInterface { public function __construct( - private readonly EntityManagerInterface $em, - private readonly ShortUrlRelationResolverInterface $relationResolver, - private readonly ShortCodeUniquenessHelperInterface $shortCodeHelper, - private readonly DoctrineBatchHelperInterface $batchHelper, + private EntityManagerInterface $em, + private ShortUrlRelationResolverInterface $relationResolver, + private ShortCodeUniquenessHelperInterface $shortCodeHelper, + private DoctrineBatchHelperInterface $batchHelper, ) { } diff --git a/module/Core/src/ShortUrl/ShortUrlResolver.php b/module/Core/src/ShortUrl/ShortUrlResolver.php index 0f32768d..408988a5 100644 --- a/module/Core/src/ShortUrl/ShortUrlResolver.php +++ b/module/Core/src/ShortUrl/ShortUrlResolver.php @@ -4,18 +4,17 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\ShortUrl; -use Doctrine\ORM\EntityManagerInterface; use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; -use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository; +use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; readonly class ShortUrlResolver implements ShortUrlResolverInterface { public function __construct( - private EntityManagerInterface $em, + private ShortUrlRepositoryInterface $repo, private UrlShortenerOptions $urlShortenerOptions, ) { } @@ -25,9 +24,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface */ public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl { - /** @var ShortUrlRepository $shortUrlRepo */ - $shortUrlRepo = $this->em->getRepository(ShortUrl::class); - $shortUrl = $shortUrlRepo->findOne($identifier, $apiKey?->spec()); + $shortUrl = $this->repo->findOne($identifier, $apiKey?->spec()); if ($shortUrl === null) { throw ShortUrlNotFoundException::fromNotFound($identifier); } @@ -53,9 +50,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface */ public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl { - /** @var ShortUrlRepository $shortUrlRepo */ - $shortUrlRepo = $this->em->getRepository(ShortUrl::class); - $shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode); + $shortUrl = $this->repo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode); if ($shortUrl === null) { throw ShortUrlNotFoundException::fromNotFound($identifier); } diff --git a/module/Core/test/ShortUrl/ShortUrlResolverTest.php b/module/Core/test/ShortUrl/ShortUrlResolverTest.php index 24571b41..4d199d67 100644 --- a/module/Core/test/ShortUrl/ShortUrlResolverTest.php +++ b/module/Core/test/ShortUrl/ShortUrlResolverTest.php @@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl; use Cake\Chronos\Chronos; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\ORM\EntityManagerInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\Test; @@ -31,14 +30,12 @@ use function range; class ShortUrlResolverTest extends TestCase { private ShortUrlResolver $urlResolver; - private MockObject & EntityManagerInterface $em; private MockObject & ShortUrlRepository $repo; protected function setUp(): void { - $this->em = $this->createMock(EntityManagerInterface::class); $this->repo = $this->createMock(ShortUrlRepository::class); - $this->urlResolver = new ShortUrlResolver($this->em, new UrlShortenerOptions()); + $this->urlResolver = new ShortUrlResolver($this->repo, new UrlShortenerOptions()); } #[Test, DataProviderExternal(ApiKeyDataProviders::class, 'adminApiKeysProvider')] @@ -51,7 +48,6 @@ class ShortUrlResolverTest extends TestCase $this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn( $shortUrl, ); - $this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo); $result = $this->urlResolver->resolveShortUrl($identifier, $apiKey); @@ -65,7 +61,6 @@ class ShortUrlResolverTest extends TestCase $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode); $this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(null); - $this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo); $this->expectException(ShortUrlNotFoundException::class); @@ -82,7 +77,6 @@ class ShortUrlResolverTest extends TestCase ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlMode::STRICT, )->willReturn($shortUrl); - $this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo); $result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)); @@ -98,7 +92,6 @@ class ShortUrlResolverTest extends TestCase ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlMode::STRICT, )->willReturn(null); - $this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo); $this->expectException(ShortUrlNotFoundException::class); @@ -120,7 +113,6 @@ class ShortUrlResolverTest extends TestCase ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlMode::STRICT, )->willReturn($shortUrl); - $this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo); $this->expectException(ShortUrlNotFoundException::class);