From 181ff164091d42e6aef8ebf7ce616d4f83fd1ce3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 22 Mar 2020 17:05:59 +0100 Subject: [PATCH] Registered PersistenceDomainResolver as a service to avoid instantiating a new one on every ShortUrl creation --- module/Core/config/dependencies.config.php | 7 ++++++- module/Core/src/Service/UrlShortener.php | 9 ++++++--- module/Core/test/Service/UrlShortenerTest.php | 7 ++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 82bec10e..6f33587a 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -9,6 +9,7 @@ use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Mezzio\Router\RouterInterface; use Mezzio\Template\TemplateRendererInterface; use Psr\EventDispatcher\EventDispatcherInterface; +use Shlinkio\Shlink\Core\Domain\Resolver; use Shlinkio\Shlink\Core\ErrorHandler; use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions; @@ -39,6 +40,8 @@ return [ Action\QrCodeAction::class => ConfigAbstractFactory::class, Middleware\QrCodeCacheMiddleware::class => ConfigAbstractFactory::class, + + Resolver\PersistenceDomainResolver::class => ConfigAbstractFactory::class, ], ], @@ -51,7 +54,7 @@ return [ Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'], Options\UrlShortenerOptions::class => ['config.url_shortener'], - Service\UrlShortener::class => [Util\UrlValidator::class, 'em'], + Service\UrlShortener::class => [Util\UrlValidator::class, 'em', Resolver\PersistenceDomainResolver::class], Service\VisitsTracker::class => ['em', EventDispatcherInterface::class], Service\ShortUrlService::class => ['em', Service\ShortUrl\ShortUrlResolver::class], Service\VisitService::class => ['em'], @@ -84,6 +87,8 @@ return [ ], Middleware\QrCodeCacheMiddleware::class => [Cache::class], + + Resolver\PersistenceDomainResolver::class => ['em'], ], ]; diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index 821c8e90..4544bfc0 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Service; use Doctrine\ORM\EntityManagerInterface; use Psr\Http\Message\UriInterface; -use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver; +use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; @@ -24,13 +24,16 @@ class UrlShortener implements UrlShortenerInterface private EntityManagerInterface $em; private UrlValidatorInterface $urlValidator; + private DomainResolverInterface $domainResolver; public function __construct( UrlValidatorInterface $urlValidator, - EntityManagerInterface $em + EntityManagerInterface $em, + DomainResolverInterface $domainResolver ) { $this->urlValidator = $urlValidator; $this->em = $em; + $this->domainResolver = $domainResolver; } /** @@ -51,7 +54,7 @@ class UrlShortener implements UrlShortenerInterface $this->urlValidator->validateUrl($url); $this->em->beginTransaction(); - $shortUrl = new ShortUrl($url, $meta, new PersistenceDomainResolver($this->em)); + $shortUrl = new ShortUrl($url, $meta, $this->domainResolver); $shortUrl->setTags($this->tagNamesToEntities($this->em, $tags)); try { diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 0e855d90..2c67bf27 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -13,6 +13,7 @@ use Laminas\Diactoros\Uri; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; @@ -53,7 +54,11 @@ class UrlShortenerTest extends TestCase $repo->shortCodeIsInUse(Argument::cetera())->willReturn(false); $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); - $this->urlShortener = new UrlShortener($this->urlValidator->reveal(), $this->em->reveal()); + $this->urlShortener = new UrlShortener( + $this->urlValidator->reveal(), + $this->em->reveal(), + new SimpleDomainResolver(), + ); } /** @test */