mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Moved process of sluggifying custom slug to a filter
This commit is contained in:
@@ -150,12 +150,4 @@ final class ShortUrlMeta
|
||||
{
|
||||
return $this->findIfExists;
|
||||
}
|
||||
|
||||
public function withCustomSlug(string $customSlug): self
|
||||
{
|
||||
$clone = clone $this;
|
||||
$clone->customSlug = $customSlug;
|
||||
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Service;
|
||||
|
||||
use Cocur\Slugify\SlugifyInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
@@ -34,21 +33,14 @@ class UrlShortener implements UrlShortenerInterface
|
||||
private $httpClient;
|
||||
/** @var EntityManagerInterface */
|
||||
private $em;
|
||||
/** @var SlugifyInterface */
|
||||
private $slugger;
|
||||
/** @var UrlShortenerOptions */
|
||||
private $options;
|
||||
|
||||
public function __construct(
|
||||
ClientInterface $httpClient,
|
||||
EntityManagerInterface $em,
|
||||
UrlShortenerOptions $options,
|
||||
SlugifyInterface $slugger
|
||||
) {
|
||||
public function __construct(ClientInterface $httpClient, EntityManagerInterface $em, UrlShortenerOptions $options)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
$this->em = $em;
|
||||
$this->options = $options;
|
||||
$this->slugger = $slugger;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +55,7 @@ class UrlShortener implements UrlShortenerInterface
|
||||
if ($this->options->isUrlValidationEnabled()) {
|
||||
$this->checkUrlExists($url);
|
||||
}
|
||||
$meta = $this->processCustomSlug($meta);
|
||||
$this->verifyCustomSlug($meta);
|
||||
|
||||
// Transactionally insert the short url, then generate the short code and finally update the short code
|
||||
try {
|
||||
@@ -123,15 +115,13 @@ class UrlShortener implements UrlShortenerInterface
|
||||
return $chars[(int) $id] . $code;
|
||||
}
|
||||
|
||||
private function processCustomSlug(ShortUrlMeta $meta): ?ShortUrlMeta
|
||||
private function verifyCustomSlug(ShortUrlMeta $meta): void
|
||||
{
|
||||
if (! $meta->hasCustomSlug()) {
|
||||
return $meta;
|
||||
return;
|
||||
}
|
||||
|
||||
// FIXME If the slug was generated while filtering the value originally, we would not need an immutable setter
|
||||
// in ShortUrlMeta
|
||||
$customSlug = $this->slugger->slugify($meta->getCustomSlug());
|
||||
$customSlug = $meta->getCustomSlug();
|
||||
|
||||
/** @var ShortUrlRepository $repo */
|
||||
$repo = $this->em->getRepository(ShortUrl::class);
|
||||
@@ -139,8 +129,6 @@ class UrlShortener implements UrlShortenerInterface
|
||||
if ($shortUrlsCount > 0) {
|
||||
throw NonUniqueSlugException::fromSlug($customSlug);
|
||||
}
|
||||
|
||||
return $meta->withCustomSlug($customSlug);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,13 +4,13 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\Core\Validation;
|
||||
|
||||
use DateTime;
|
||||
use Shlinkio\Shlink\Common\Validation\InputFactoryTrait;
|
||||
use Shlinkio\Shlink\Common\Validation;
|
||||
use Zend\InputFilter\InputFilter;
|
||||
use Zend\Validator;
|
||||
|
||||
class ShortUrlMetaInputFilter extends InputFilter
|
||||
{
|
||||
use InputFactoryTrait;
|
||||
use Validation\InputFactoryTrait;
|
||||
|
||||
public const VALID_SINCE = 'validSince';
|
||||
public const VALID_UNTIL = 'validUntil';
|
||||
@@ -36,7 +36,9 @@ class ShortUrlMetaInputFilter extends InputFilter
|
||||
$validUntil->getValidatorChain()->attach(new Validator\Date(['format' => DateTime::ATOM]));
|
||||
$this->add($validUntil);
|
||||
|
||||
$this->add($this->createInput(self::CUSTOM_SLUG, false));
|
||||
$customSlug = $this->createInput(self::CUSTOM_SLUG, false);
|
||||
$customSlug->getFilterChain()->attach(new Validation\SluggerFilter());
|
||||
$this->add($customSlug);
|
||||
|
||||
$maxVisits = $this->createInput(self::MAX_VISITS, false);
|
||||
$maxVisits->getValidatorChain()->attach(new Validator\Digits())
|
||||
|
||||
Reference in New Issue
Block a user