Added simple way to resolve domains from entity manager when creating a short URL

This commit is contained in:
Alejandro Celaya 2019-10-01 20:16:27 +02:00
parent 1085809fa5
commit d0bb86ca8f
8 changed files with 69 additions and 10 deletions

View File

@ -64,4 +64,5 @@ $builder->createManyToMany('tags', Entity\Tag::class)
$builder->createManyToOne('domain', Entity\Domain::class) $builder->createManyToOne('domain', Entity\Domain::class)
->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT') ->addJoinColumn('domain_id', 'id', true, false, 'RESTRICT')
->cascadePersist()
->build(); ->build();

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
interface DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain;
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
class PersistenceDomainResolver implements DomainResolverInterface
{
/** @var EntityManagerInterface */
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->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);
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class SimpleDomainResolver implements DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain
{
return $domain !== null ? new Domain($domain) : null;
}
}

View File

@ -7,6 +7,8 @@ use Cake\Chronos\Chronos;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Shlinkio\Shlink\Common\Entity\AbstractEntity; use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
@ -33,8 +35,11 @@ class ShortUrl extends AbstractEntity
/** @var Domain|null */ /** @var Domain|null */
private $domain; private $domain;
public function __construct(string $longUrl, ?ShortUrlMeta $meta = null) public function __construct(
{ string $longUrl,
?ShortUrlMeta $meta = null,
?DomainResolverInterface $domainResolver = null
) {
$meta = $meta ?? ShortUrlMeta::createEmpty(); $meta = $meta ?? ShortUrlMeta::createEmpty();
$this->longUrl = $longUrl; $this->longUrl = $longUrl;
@ -45,7 +50,12 @@ class ShortUrl extends AbstractEntity
$this->validUntil = $meta->getValidUntil(); $this->validUntil = $meta->getValidUntil();
$this->maxVisits = $meta->getMaxVisits(); $this->maxVisits = $meta->getMaxVisits();
$this->shortCode = $meta->getCustomSlug() ?? ''; // TODO logic to calculate short code should be passed somehow $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 public function getLongUrl(): string

View File

@ -151,11 +151,6 @@ final class ShortUrlMeta
return (bool) $this->findIfExists; return (bool) $this->findIfExists;
} }
public function hasDomain(): bool
{
return $this->domain !== null;
}
public function getDomain(): ?string public function getDomain(): ?string
{ {
return $this->domain; return $this->domain;

View File

@ -9,6 +9,7 @@ use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
@ -77,7 +78,7 @@ class UrlShortener implements UrlShortenerInterface
$this->em->beginTransaction(); $this->em->beginTransaction();
// First, create the short URL with an empty short code // 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->persist($shortUrl);
$this->em->flush(); $this->em->flush();

View File

@ -18,7 +18,7 @@ trait TagManagerTrait
* @param string[] $tags * @param string[] $tags
* @return Collections\Collection|Tag[] * @return Collections\Collection|Tag[]
*/ */
private function tagNamesToEntities(EntityManagerInterface $em, array $tags) private function tagNamesToEntities(EntityManagerInterface $em, array $tags): Collections\Collection
{ {
$entities = []; $entities = [];
foreach ($tags as $tagName) { foreach ($tags as $tagName) {