From d0bb86ca8fdf985c80a0e9f4ac245915550ded32 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 1 Oct 2019 20:16:27 +0200 Subject: [PATCH] Added simple way to resolve domains from entity manager when creating a short URL --- .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 1 + .../Resolver/DomainResolverInterface.php | 11 ++++++++ .../Resolver/PersistenceDomainResolver.php | 27 +++++++++++++++++++ .../Domain/Resolver/SimpleDomainResolver.php | 14 ++++++++++ module/Core/src/Entity/ShortUrl.php | 16 ++++++++--- module/Core/src/Model/ShortUrlMeta.php | 5 ---- module/Core/src/Service/UrlShortener.php | 3 ++- module/Core/src/Util/TagManagerTrait.php | 2 +- 8 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 module/Core/src/Domain/Resolver/DomainResolverInterface.php create mode 100644 module/Core/src/Domain/Resolver/PersistenceDomainResolver.php create mode 100644 module/Core/src/Domain/Resolver/SimpleDomainResolver.php diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index 00c03b30..c6db2003 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -64,4 +64,5 @@ $builder->createManyToMany('tags', Entity\Tag::class) $builder->createManyToOne('domain', Entity\Domain::class) ->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT') + ->cascadePersist() ->build(); diff --git a/module/Core/src/Domain/Resolver/DomainResolverInterface.php b/module/Core/src/Domain/Resolver/DomainResolverInterface.php new file mode 100644 index 00000000..af23aa37 --- /dev/null +++ b/module/Core/src/Domain/Resolver/DomainResolverInterface.php @@ -0,0 +1,11 @@ +em = $em; + } + + public function resolveDomain(?string $domain): ?Domain + { + if ($domain === null) { + return null; + } + + return $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]) ?? new Domain($domain); + } +} diff --git a/module/Core/src/Domain/Resolver/SimpleDomainResolver.php b/module/Core/src/Domain/Resolver/SimpleDomainResolver.php new file mode 100644 index 00000000..924ca17f --- /dev/null +++ b/module/Core/src/Domain/Resolver/SimpleDomainResolver.php @@ -0,0 +1,14 @@ +longUrl = $longUrl; @@ -45,7 +50,12 @@ class ShortUrl extends AbstractEntity $this->validUntil = $meta->getValidUntil(); $this->maxVisits = $meta->getMaxVisits(); $this->shortCode = $meta->getCustomSlug() ?? ''; // TODO logic to calculate short code should be passed somehow - $this->domain = $meta->hasDomain() ? new Domain($meta->getDomain()) : null; + $this->domain = $this->domainToEntity($meta->getDomain(), $domainResolver ?? new SimpleDomainResolver()); + } + + private function domainToEntity(?string $domain, DomainResolverInterface $domainResolver): ?Domain + { + return $domainResolver->resolveDomain($domain); } public function getLongUrl(): string diff --git a/module/Core/src/Model/ShortUrlMeta.php b/module/Core/src/Model/ShortUrlMeta.php index b6d00834..37dca315 100644 --- a/module/Core/src/Model/ShortUrlMeta.php +++ b/module/Core/src/Model/ShortUrlMeta.php @@ -151,11 +151,6 @@ final class ShortUrlMeta return (bool) $this->findIfExists; } - public function hasDomain(): bool - { - return $this->domain !== null; - } - public function getDomain(): ?string { return $this->domain; diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index fe041900..8b456010 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -9,6 +9,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\RequestOptions; use Psr\Http\Message\UriInterface; +use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; @@ -77,7 +78,7 @@ class UrlShortener implements UrlShortenerInterface $this->em->beginTransaction(); // First, create the short URL with an empty short code - $shortUrl = new ShortUrl($url, $meta); + $shortUrl = new ShortUrl($url, $meta, new PersistenceDomainResolver($this->em)); $this->em->persist($shortUrl); $this->em->flush(); diff --git a/module/Core/src/Util/TagManagerTrait.php b/module/Core/src/Util/TagManagerTrait.php index 854a98b6..af6a8658 100644 --- a/module/Core/src/Util/TagManagerTrait.php +++ b/module/Core/src/Util/TagManagerTrait.php @@ -18,7 +18,7 @@ trait TagManagerTrait * @param string[] $tags * @return Collections\Collection|Tag[] */ - private function tagNamesToEntities(EntityManagerInterface $em, array $tags) + private function tagNamesToEntities(EntityManagerInterface $em, array $tags): Collections\Collection { $entities = []; foreach ($tags as $tagName) {