mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Added helper service to avoid code duplication when resolving short URLs titles
This commit is contained in:
@@ -15,26 +15,26 @@ use Shlinkio\Shlink\Core\Model\ShortUrlsParams;
|
||||
use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter;
|
||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class ShortUrlService implements ShortUrlServiceInterface
|
||||
{
|
||||
private ORM\EntityManagerInterface $em;
|
||||
private ShortUrlResolverInterface $urlResolver;
|
||||
private UrlValidatorInterface $urlValidator;
|
||||
private ShortUrlTitleResolutionHelperInterface $titleResolutionHelper;
|
||||
private ShortUrlRelationResolverInterface $relationResolver;
|
||||
|
||||
public function __construct(
|
||||
ORM\EntityManagerInterface $em,
|
||||
ShortUrlResolverInterface $urlResolver,
|
||||
UrlValidatorInterface $urlValidator,
|
||||
ShortUrlTitleResolutionHelperInterface $titleResolutionHelper,
|
||||
ShortUrlRelationResolverInterface $relationResolver
|
||||
) {
|
||||
$this->em = $em;
|
||||
$this->urlResolver = $urlResolver;
|
||||
$this->urlValidator = $urlValidator;
|
||||
$this->titleResolutionHelper = $titleResolutionHelper;
|
||||
$this->relationResolver = $relationResolver;
|
||||
}
|
||||
|
||||
@@ -62,7 +62,8 @@ class ShortUrlService implements ShortUrlServiceInterface
|
||||
?ApiKey $apiKey = null
|
||||
): ShortUrl {
|
||||
if ($shortUrlEdit->longUrlWasProvided()) {
|
||||
$shortUrlEdit = $this->processTitleAndValidateUrl($shortUrlEdit);
|
||||
/** @var ShortUrlEdit $shortUrlEdit */
|
||||
$shortUrlEdit = $this->titleResolutionHelper->processTitleAndValidateUrl($shortUrlEdit);
|
||||
}
|
||||
|
||||
$shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey);
|
||||
@@ -72,15 +73,4 @@ class ShortUrlService implements ShortUrlServiceInterface
|
||||
|
||||
return $shortUrl;
|
||||
}
|
||||
|
||||
private function processTitleAndValidateUrl(ShortUrlEdit $shortUrlEdit): ShortUrlEdit
|
||||
{
|
||||
if ($shortUrlEdit->titleWasProvided()) {
|
||||
$this->urlValidator->validateUrl($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
|
||||
return $shortUrlEdit;
|
||||
}
|
||||
|
||||
$title = $this->urlValidator->validateUrlWithTitle($shortUrlEdit->longUrl(), $shortUrlEdit->doValidateUrl());
|
||||
return $title === null ? $shortUrlEdit : $shortUrlEdit->withResolvedTitle($title);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,23 +11,23 @@ use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlTitleResolutionHelperInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
|
||||
|
||||
class UrlShortener implements UrlShortenerInterface
|
||||
{
|
||||
private EntityManagerInterface $em;
|
||||
private UrlValidatorInterface $urlValidator;
|
||||
private ShortUrlTitleResolutionHelperInterface $titleResolutionHelper;
|
||||
private ShortUrlRelationResolverInterface $relationResolver;
|
||||
private ShortCodeHelperInterface $shortCodeHelper;
|
||||
|
||||
public function __construct(
|
||||
UrlValidatorInterface $urlValidator,
|
||||
ShortUrlTitleResolutionHelperInterface $titleResolutionHelper,
|
||||
EntityManagerInterface $em,
|
||||
ShortUrlRelationResolverInterface $relationResolver,
|
||||
ShortCodeHelperInterface $shortCodeHelper
|
||||
) {
|
||||
$this->urlValidator = $urlValidator;
|
||||
$this->titleResolutionHelper = $titleResolutionHelper;
|
||||
$this->em = $em;
|
||||
$this->relationResolver = $relationResolver;
|
||||
$this->shortCodeHelper = $shortCodeHelper;
|
||||
@@ -45,7 +45,8 @@ class UrlShortener implements UrlShortenerInterface
|
||||
return $existingShortUrl;
|
||||
}
|
||||
|
||||
$meta = $this->processTitleAndValidateUrl($meta);
|
||||
/** @var ShortUrlMeta $meta */
|
||||
$meta = $this->titleResolutionHelper->processTitleAndValidateUrl($meta);
|
||||
|
||||
return $this->em->transactional(function () use ($meta) {
|
||||
$shortUrl = ShortUrl::fromMeta($meta, $this->relationResolver);
|
||||
@@ -82,15 +83,4 @@ class UrlShortener implements UrlShortenerInterface
|
||||
throw NonUniqueSlugException::fromSlug($shortUrlToBeCreated->getShortCode(), $domainAuthority);
|
||||
}
|
||||
}
|
||||
|
||||
private function processTitleAndValidateUrl(ShortUrlMeta $meta): ShortUrlMeta
|
||||
{
|
||||
if ($meta->hasTitle()) {
|
||||
$this->urlValidator->validateUrl($meta->getLongUrl(), $meta->doValidateUrl());
|
||||
return $meta;
|
||||
}
|
||||
|
||||
$title = $this->urlValidator->validateUrlWithTitle($meta->getLongUrl(), $meta->doValidateUrl());
|
||||
return $title === null ? $meta : $meta->withResolvedTitle($title);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user