2017-10-21 18:18:57 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-21 18:18:57 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core\Exception;
|
|
|
|
|
2019-11-25 01:32:37 +03:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2020-01-01 23:11:53 +03:00
|
|
|
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
|
|
|
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
2021-04-18 18:07:56 +03:00
|
|
|
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
|
2019-11-25 01:32:37 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
2019-11-25 01:32:37 +03:00
|
|
|
class NonUniqueSlugException extends InvalidArgumentException implements ProblemDetailsExceptionInterface
|
2017-10-21 18:18:57 +03:00
|
|
|
{
|
2019-11-25 01:32:37 +03:00
|
|
|
use CommonProblemDetailsExceptionTrait;
|
|
|
|
|
|
|
|
private const TITLE = 'Invalid custom slug';
|
2019-11-27 00:12:52 +03:00
|
|
|
private const TYPE = 'INVALID_SLUG';
|
2019-11-25 01:32:37 +03:00
|
|
|
|
2019-11-28 20:42:27 +03:00
|
|
|
public static function fromSlug(string $slug, ?string $domain = null): self
|
2017-10-21 18:18:57 +03:00
|
|
|
{
|
2019-11-25 20:54:25 +03:00
|
|
|
$suffix = $domain === null ? '' : sprintf(' for domain "%s"', $domain);
|
2019-11-25 01:32:37 +03:00
|
|
|
$e = new self(sprintf('Provided slug "%s" is already in use%s.', $slug, $suffix));
|
|
|
|
|
|
|
|
$e->detail = $e->getMessage();
|
|
|
|
$e->title = self::TITLE;
|
|
|
|
$e->type = self::TYPE;
|
|
|
|
$e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
|
2019-11-27 22:18:36 +03:00
|
|
|
$e->additional = ['customSlug' => $slug];
|
|
|
|
|
|
|
|
if ($domain !== null) {
|
|
|
|
$e->additional['domain'] = $domain;
|
|
|
|
}
|
2019-11-25 01:32:37 +03:00
|
|
|
|
|
|
|
return $e;
|
2017-10-21 18:18:57 +03:00
|
|
|
}
|
2021-04-18 18:07:56 +03:00
|
|
|
|
|
|
|
public static function fromImport(ImportedShlinkUrl $importedUrl): self
|
|
|
|
{
|
2022-08-07 10:36:51 +03:00
|
|
|
return self::fromSlug($importedUrl->shortCode, $importedUrl->domain);
|
2021-04-18 18:07:56 +03:00
|
|
|
}
|
2017-10-21 18:18:57 +03:00
|
|
|
}
|