mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Added helper service to avoid code duplication when resolving short URLs titles
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user