shlink/module/Core/src/Exception/NonUniqueSlugException.php

44 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Exception;
use Fig\Http\Message\StatusCodeInterface;
2020-01-01 23:11:53 +03:00
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use function sprintf;
class NonUniqueSlugException extends InvalidArgumentException implements ProblemDetailsExceptionInterface
{
use CommonProblemDetailsExceptionTrait;
private const TITLE = 'Invalid custom slug';
2019-11-27 00:12:52 +03:00
private const TYPE = 'INVALID_SLUG';
public static function fromSlug(string $slug, ?string $domain = null): self
{
$suffix = $domain === null ? '' : sprintf(' for domain "%s"', $domain);
$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;
$e->additional = ['customSlug' => $slug];
if ($domain !== null) {
$e->additional['domain'] = $domain;
}
return $e;
}
public static function fromImport(ImportedShlinkUrl $importedUrl): self
{
return self::fromSlug($importedUrl->shortCode(), $importedUrl->domain());
}
}