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