diff --git a/src/AuthorizationService.php b/src/AuthorizationService.php index ef4c01eb..b6a11c90 100644 --- a/src/AuthorizationService.php +++ b/src/AuthorizationService.php @@ -22,6 +22,7 @@ use Authorization\Policy\ResolverInterface; use Authorization\Policy\Result; use Authorization\Policy\ResultInterface; +use Closure; class AuthorizationService implements AuthorizationServiceInterface { @@ -84,33 +85,22 @@ protected function performCheck(?IdentityInterface $user, string $action, mixed $result = $policy->before($user, $resource, $action); if ($result !== null) { - return $this->resultTypeCheck($result); + return $result; } } $handler = $this->getCanHandler($policy, $action); $result = $handler($user, $resource); - return $this->resultTypeCheck($result); - } - - /** - * Check result type. - * - * @param mixed $result Result from policy class instance. - * @return \Authorization\Policy\ResultInterface|bool - * @throws \Authorization\Exception\Exception If $result argument is not a boolean or ResultInterface instance. - */ - protected function resultTypeCheck(mixed $result): ResultInterface|bool - { - if (is_bool($result) || $result instanceof ResultInterface) { - return $result; - } + assert( + is_bool($result) || $result instanceof ResultInterface, + new Exception(sprintf( + 'Authorization check method must return `%s` or `bool`.', + ResultInterface::class + )) + ); - throw new Exception(sprintf( - 'Pre-authorization check must return `%s`, `bool` or `null`.', - ResultInterface::class - )); + return $result; } /** @@ -130,18 +120,19 @@ public function applyScope(?IdentityInterface $user, string $action, $resource): * * @param mixed $policy Policy object. * @param string $action Action name. - * @return callable + * @return \Closure * @throws \Authorization\Policy\Exception\MissingMethodException */ - protected function getCanHandler(mixed $policy, string $action): callable + protected function getCanHandler(mixed $policy, string $action): Closure { $method = 'can' . ucfirst($action); - if (!method_exists($policy, $method) && !method_exists($policy, '__call')) { - throw new MissingMethodException([$method, $action, get_class($policy)]); - } + assert( + method_exists($policy, $method) || method_exists($policy, '__call'), + new MissingMethodException([$method, $action, get_class($policy)]) + ); - return [$policy, $method]; + return [$policy, $method](...); } /** @@ -149,18 +140,19 @@ protected function getCanHandler(mixed $policy, string $action): callable * * @param mixed $policy Policy object. * @param string $action Action name. - * @return callable + * @return \Closure * @throws \Authorization\Policy\Exception\MissingMethodException */ - protected function getScopeHandler(mixed $policy, string $action): callable + protected function getScopeHandler(mixed $policy, string $action): Closure { $method = 'scope' . ucfirst($action); - if (!method_exists($policy, $method)) { - throw new MissingMethodException([$method, $action, get_class($policy)]); - } + assert( + method_exists($policy, $method) || method_exists($policy, '__call'), + new MissingMethodException([$method, $action, get_class($policy)]) + ); - return [$policy, $method]; + return [$policy, $method](...); } /** diff --git a/src/Middleware/AuthorizationMiddleware.php b/src/Middleware/AuthorizationMiddleware.php index d309d01c..337a050b 100644 --- a/src/Middleware/AuthorizationMiddleware.php +++ b/src/Middleware/AuthorizationMiddleware.php @@ -179,7 +179,7 @@ protected function buildIdentity( if (!$identity instanceof IdentityInterface) { throw new RuntimeException(sprintf( 'Invalid identity returned by decorator. `%s` does not implement `%s`.', - is_object($identity) ? get_class($identity) : gettype($identity), + get_debug_type($identity), IdentityInterface::class )); }