OAuthMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 17 4
1
<?php
2
3
namespace PHPHub\Http\Middleware;
4
5
use Closure;
6
use Dingo\Api\Routing\Router;
7
use Dingo\Api\Auth\Auth as Authentication;
8
use League\OAuth2\Server\Exception\AccessDeniedException;
9
use LucaDegasperi\OAuth2Server\Authorizer;
10
11
class OAuthMiddleware
12
{
13
    protected $authorizer;
14
    private $auth;
15
    private $router;
16
17
    /**
18
     * Create a new auth middleware instance.
19
     *
20
     * @param \Dingo\Api\Routing\Router $router
21
     * @param \Dingo\Api\Auth\Auth      $auth
22
     * @param Authorizer                $authorizer
23
     */
24
    public function __construct(Router $router, Authentication $auth, Authorizer $authorizer)
25
    {
26
        $this->router = $router;
27
        $this->auth = $auth;
28
        $this->authorizer = $authorizer;
29
    }
30
31
    /**
32
     * Handle an incoming request.
33
     *
34
     * @param \Illuminate\Http\Request                $request
35
     * @param Closure|\PHPHub\Http\Middleware\Closure $next
36
     * @param $type
37
     *
38
     * @return mixed
39
     *
40
     * @throws AccessDeniedException
41
     */
42
    public function handle($request, Closure $next, $type = null)
43
    {
44
        $route = $this->router->getCurrentRoute();
45
46
        if (! $this->auth->check(false)) {
47
            $this->auth->authenticate($route->getAuthProviders());
48
        }
49
50
        $this->authorizer->setRequest($request);
51
52
        //type: user or client
53
        if ($type && $this->authorizer->getResourceOwnerType() !== $type) {
54
            throw new AccessDeniedException();
55
        }
56
57
        return $next($request);
58
    }
59
}
60