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) {