Total Complexity | 5 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Coverage | 91.67% |
Changes | 0 |
1 | <?php |
||
19 | abstract class AuthorizerMiddleware |
||
20 | { |
||
21 | protected $authorizer; |
||
22 | |||
23 | private $event; |
||
24 | |||
25 | 8 | public function __construct(Authorizer $authorizer, Dispatcher $event) |
|
29 | 8 | } |
|
30 | |||
31 | abstract protected function authorized(GrantableOwner $owner, array $grantables): bool; |
||
32 | |||
33 | 8 | public function handle(Request $request, Closure $next, string ...$grantables) |
|
34 | { |
||
35 | 8 | $authenticated = $request->user(); |
|
36 | |||
37 | 8 | if ($this->isAuthorizedRequestFor($authenticated)($grantables)) { |
|
38 | return $next($request); |
||
39 | } |
||
40 | |||
41 | 8 | $this->event->dispatch(new UnauthorizedOwner($authenticated, $grantables)); |
|
42 | |||
43 | 8 | throw new UnauthorizedOwnerException($authenticated); |
|
44 | } |
||
45 | |||
46 | private function isAuthorizedRequestFor(Model $authenticated): Closure |
||
50 | 8 | }; |
|
51 | } |
||
53 |