Skip to content

Commit

Permalink
♻️ scope slugs can be enums
Browse files Browse the repository at this point in the history
  • Loading branch information
M4tini committed Jan 18, 2025
1 parent f9aa7e9 commit 355595e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/Exceptions/InvalidScopeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@

namespace MyParcelCom\JsonApi\Exceptions;

use BackedEnum;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
use UnitEnum;

/**
* This exception is thrown when a scope is either not available at all, not unavailable for the chosen grant type or
* not attached to the requesting client.
*/
class InvalidScopeException extends AbstractException
{
public function __construct(array $slugs, Throwable $previous = null)
public function __construct(array $scopeSlugs, Throwable $previous = null)
{
$scopeStrings = collect($scopeSlugs)
->map(fn ($scope) => match (true) {
$scope instanceof BackedEnum => $scope->value,
$scope instanceof UnitEnum => $scope->name,
default => $scope,
})
->toArray();

parent::__construct(
'The following scopes are not available to the requesting client: ' . implode(', ', $slugs),
'The following scopes are not available to the requesting client: ' . implode(', ', $scopeStrings),
self::AUTH_INVALID_SCOPE,
Response::HTTP_FORBIDDEN,
$previous,
Expand Down
14 changes: 12 additions & 2 deletions src/Exceptions/MissingScopeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

namespace MyParcelCom\JsonApi\Exceptions;

use BackedEnum;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
use UnitEnum;

class MissingScopeException extends AbstractException
{
public function __construct(array $scopes, Throwable $previous = null)
public function __construct(array $scopeSlugs, Throwable $previous = null)
{
$scopeStrings = collect($scopeSlugs)
->map(fn ($scope) => match (true) {
$scope instanceof BackedEnum => $scope->value,
$scope instanceof UnitEnum => $scope->name,
default => $scope,
})
->toArray();

parent::__construct(
'The used access token does not contain the required scope(s): ' . implode(', ', $scopes) . '.',
'The used access token does not contain the required scope(s): ' . implode(', ', $scopeStrings) . '.',
self::AUTH_MISSING_SCOPE,
Response::HTTP_FORBIDDEN,
$previous,
Expand Down

0 comments on commit 355595e

Please sign in to comment.