Added helper service to avoid code duplication when resolving short URLs titles

This commit is contained in:
Alejandro Celaya
2021-02-05 17:59:34 +01:00
parent 71e91a541f
commit 608742c2e2
11 changed files with 159 additions and 81 deletions

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Shlinkio\Shlink\Core\Util\UrlValidatorInterface;
class ShortUrlTitleResolutionHelper implements ShortUrlTitleResolutionHelperInterface
{
private UrlValidatorInterface $urlValidator;
public function __construct(UrlValidatorInterface $urlValidator)
{
$this->urlValidator = $urlValidator;
}
public function processTitleAndValidateUrl(TitleResolutionModelInterface $data): TitleResolutionModelInterface
{
if ($data->hasTitle()) {
$this->urlValidator->validateUrl($data->getLongUrl(), $data->doValidateUrl());
return $data;
}
$title = $this->urlValidator->validateUrlWithTitle($data->getLongUrl(), $data->doValidateUrl());
return $title === null ? $data : $data->withResolvedTitle($title);
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
interface ShortUrlTitleResolutionHelperInterface
{
/**
* @throws InvalidUrlException
*/
public function processTitleAndValidateUrl(TitleResolutionModelInterface $data): TitleResolutionModelInterface;
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
interface TitleResolutionModelInterface
{
public function hasTitle(): bool;
public function getLongUrl(): string;
public function doValidateUrl(): ?bool;
public function withResolvedTitle(string $title): self;
}