Inject ShortUrlRepository in ShortUrlResolver

This commit is contained in:
Alejandro Celaya 2024-11-09 09:39:56 +01:00
parent 102169b6c7
commit 532102e662
4 changed files with 18 additions and 24 deletions

View File

@ -50,6 +50,10 @@ return [
ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class, ShortUrl\Transformer\ShortUrlDataTransformer::class => ConfigAbstractFactory::class,
ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class, ShortUrl\Middleware\ExtraPathRedirectMiddleware::class => ConfigAbstractFactory::class,
ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class, ShortUrl\Middleware\TrimTrailingSlashMiddleware::class => ConfigAbstractFactory::class,
ShortUrl\Repository\ShortUrlRepository::class => [
EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class,
],
ShortUrl\Repository\ShortUrlListRepository::class => [ ShortUrl\Repository\ShortUrlListRepository::class => [
EntityRepositoryFactory::class, EntityRepositoryFactory::class,
ShortUrl\Entity\ShortUrl::class, ShortUrl\Entity\ShortUrl::class,
@ -162,7 +166,10 @@ return [
ShortUrl\ShortUrlResolver::class, ShortUrl\ShortUrlResolver::class,
ShortUrl\Repository\ExpiredShortUrlsRepository::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 => [ ShortUrl\ShortUrlVisitsDeleter::class => [
Visit\Repository\VisitDeleterRepository::class, Visit\Repository\VisitDeleterRepository::class,
ShortUrl\ShortUrlResolver::class, ShortUrl\ShortUrlResolver::class,

View File

@ -25,13 +25,13 @@ use Throwable;
use function Shlinkio\Shlink\Core\normalizeDate; use function Shlinkio\Shlink\Core\normalizeDate;
use function sprintf; use function sprintf;
class ImportedLinksProcessor implements ImportedLinksProcessorInterface readonly class ImportedLinksProcessor implements ImportedLinksProcessorInterface
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $em, private EntityManagerInterface $em,
private readonly ShortUrlRelationResolverInterface $relationResolver, private ShortUrlRelationResolverInterface $relationResolver,
private readonly ShortCodeUniquenessHelperInterface $shortCodeHelper, private ShortCodeUniquenessHelperInterface $shortCodeHelper,
private readonly DoctrineBatchHelperInterface $batchHelper, private DoctrineBatchHelperInterface $batchHelper,
) { ) {
} }

View File

@ -4,18 +4,17 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl; namespace Shlinkio\Shlink\Core\ShortUrl;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; 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; use Shlinkio\Shlink\Rest\Entity\ApiKey;
readonly class ShortUrlResolver implements ShortUrlResolverInterface readonly class ShortUrlResolver implements ShortUrlResolverInterface
{ {
public function __construct( public function __construct(
private EntityManagerInterface $em, private ShortUrlRepositoryInterface $repo,
private UrlShortenerOptions $urlShortenerOptions, private UrlShortenerOptions $urlShortenerOptions,
) { ) {
} }
@ -25,9 +24,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
*/ */
public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl public function resolveShortUrl(ShortUrlIdentifier $identifier, ApiKey|null $apiKey = null): ShortUrl
{ {
/** @var ShortUrlRepository $shortUrlRepo */ $shortUrl = $this->repo->findOne($identifier, $apiKey?->spec());
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
$shortUrl = $shortUrlRepo->findOne($identifier, $apiKey?->spec());
if ($shortUrl === null) { if ($shortUrl === null) {
throw ShortUrlNotFoundException::fromNotFound($identifier); throw ShortUrlNotFoundException::fromNotFound($identifier);
} }
@ -53,9 +50,7 @@ readonly class ShortUrlResolver implements ShortUrlResolverInterface
*/ */
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl
{ {
/** @var ShortUrlRepository $shortUrlRepo */ $shortUrl = $this->repo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
$shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
if ($shortUrl === null) { if ($shortUrl === null) {
throw ShortUrlNotFoundException::fromNotFound($identifier); throw ShortUrlNotFoundException::fromNotFound($identifier);
} }

View File

@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\Test;
@ -31,14 +30,12 @@ use function range;
class ShortUrlResolverTest extends TestCase class ShortUrlResolverTest extends TestCase
{ {
private ShortUrlResolver $urlResolver; private ShortUrlResolver $urlResolver;
private MockObject & EntityManagerInterface $em;
private MockObject & ShortUrlRepository $repo; private MockObject & ShortUrlRepository $repo;
protected function setUp(): void protected function setUp(): void
{ {
$this->em = $this->createMock(EntityManagerInterface::class);
$this->repo = $this->createMock(ShortUrlRepository::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')] #[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( $this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(
$shortUrl, $shortUrl,
); );
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$result = $this->urlResolver->resolveShortUrl($identifier, $apiKey); $result = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
@ -65,7 +61,6 @@ class ShortUrlResolverTest extends TestCase
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode); $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
$this->repo->expects($this->once())->method('findOne')->with($identifier, $apiKey?->spec())->willReturn(null); $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); $this->expectException(ShortUrlNotFoundException::class);
@ -82,7 +77,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT, ShortUrlMode::STRICT,
)->willReturn($shortUrl); )->willReturn($shortUrl);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)); $result = $this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
@ -98,7 +92,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT, ShortUrlMode::STRICT,
)->willReturn(null); )->willReturn(null);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->expectException(ShortUrlNotFoundException::class); $this->expectException(ShortUrlNotFoundException::class);
@ -120,7 +113,6 @@ class ShortUrlResolverTest extends TestCase
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode), ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
ShortUrlMode::STRICT, ShortUrlMode::STRICT,
)->willReturn($shortUrl); )->willReturn($shortUrl);
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
$this->expectException(ShortUrlNotFoundException::class); $this->expectException(ShortUrlNotFoundException::class);