Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

CheckScopes   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Http\Middleware;
6
7
use Illuminate\Auth\AuthenticationException;
8
use Rinvex\Oauth\Exceptions\MissingScopeException;
9
10
class CheckScopes
11
{
12
    /**
13
     * Handle the incoming request.
14
     *
15
     * @param \Illuminate\Http\Request $request
16
     * @param \Closure                 $next
17
     * @param mixed                    ...$scopes
18
     *
19
     * @throws \Illuminate\Auth\AuthenticationException|\Rinvex\Oauth\Exceptions\MissingScopeException
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $scopes a bit more specific; maybe use array.
Loading history...
23
    public function handle($request, $next, ...$scopes)
24
    {
25
        if (! $request->user() || ! $request->user()->token()) {
26
            throw new AuthenticationException();
27
        }
28
29
        foreach ($scopes as $scope) {
30
            if (! $request->user()->token()->abilities->map->getRouteKey()->contains($scope)) {
31
                throw new MissingScopeException($scope);
32
            }
33
        }
34
35
        return $next($request);
36
    }
37
}
38