DeleteShortUrlException.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 DeleteShortUrlException extends DomainException implements ProblemDetailsExceptionInterface
  9. {
  10. use CommonProblemDetailsExceptionTrait;
  11. private const TITLE = 'Cannot delete short URL';
  12. private const TYPE = 'INVALID_SHORTCODE_DELETION'; // FIXME Should be INVALID_SHORT_URL_DELETION
  13. public static function fromVisitsThreshold(int $threshold, string $shortCode): self
  14. {
  15. $e = new self(sprintf(
  16. 'Impossible to delete short URL with short code "%s" since it has more than "%s" visits.',
  17. $shortCode,
  18. $threshold,
  19. ));
  20. $e->detail = $e->getMessage();
  21. $e->title = self::TITLE;
  22. $e->type = self::TYPE;
  23. $e->status = StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY;
  24. $e->additional = [
  25. 'shortCode' => $shortCode,
  26. 'threshold' => $threshold,
  27. ];
  28. return $e;
  29. }
  30. public function getVisitsThreshold(): int
  31. {
  32. return $this->additional['threshold'];
  33. }
  34. }