MissingAuthenticationException.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Rest\Exception;
  4. use Fig\Http\Message\StatusCodeInterface;
  5. use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
  6. use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
  7. use function implode;
  8. use function sprintf;
  9. class MissingAuthenticationException extends RuntimeException implements ProblemDetailsExceptionInterface
  10. {
  11. use CommonProblemDetailsExceptionTrait;
  12. private const TITLE = 'Invalid authorization';
  13. private const TYPE = 'INVALID_AUTHORIZATION';
  14. public static function fromExpectedTypes(array $expectedTypes): self
  15. {
  16. $e = new self(sprintf(
  17. 'Expected one of the following authentication headers, ["%s"], but none were provided',
  18. implode('", "', $expectedTypes),
  19. ));
  20. $e->detail = $e->getMessage();
  21. $e->title = self::TITLE;
  22. $e->type = self::TYPE;
  23. $e->status = StatusCodeInterface::STATUS_UNAUTHORIZED;
  24. $e->additional = ['expectedTypes' => $expectedTypes];
  25. return $e;
  26. }
  27. }