ShortUrlNotFoundException.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Core\Exception;
  4. use Fig\Http\Message\StatusCodeInterface;
  5. use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
  6. use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
  7. use function sprintf;
  8. class ShortUrlNotFoundException extends DomainException implements ProblemDetailsExceptionInterface
  9. {
  10. use CommonProblemDetailsExceptionTrait;
  11. private const TITLE = 'Short URL not found';
  12. private const TYPE = 'INVALID_SHORTCODE';
  13. public static function fromNotFoundShortCode(string $shortCode, ?string $domain = null): self
  14. {
  15. $suffix = $domain === null ? '' : sprintf(' for domain "%s"', $domain);
  16. $e = new self(sprintf('No URL found with short code "%s"%s', $shortCode, $suffix));
  17. $e->detail = $e->getMessage();
  18. $e->title = self::TITLE;
  19. $e->type = self::TYPE;
  20. $e->status = StatusCodeInterface::STATUS_NOT_FOUND;
  21. $e->additional = ['shortCode' => $shortCode];
  22. if ($domain !== null) {
  23. $e->additional['domain'] = $domain;
  24. }
  25. return $e;
  26. }
  27. }