Completed
Pull Request — master (#738)
by
unknown
62:12
created

OAuthExceptionHandlerMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 6
c 4
b 1
f 2
lcom 0
cbo 2
dl 0
loc 38
ccs 0
cts 10
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 27 6
1
<?php
2
3
/*
4
 * This file is part of OAuth 2.0 Laravel.
5
 *
6
 * (c) Luca Degasperi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LucaDegasperi\OAuth2Server\Middleware;
13
14
use Closure;
15
use Illuminate\Http\JsonResponse;
16
use League\OAuth2\Server\Exception\OAuthException;
17
18
/**
19
 * This is the exception handler middleware class.
20
 *
21
 * @author Luca Degasperi <[email protected]>
22
 */
23
class OAuthExceptionHandlerMiddleware
24
{
25
    /**
26
     * Handle an incoming request.
27
     *
28
     * @param \Illuminate\Http\Request  $request
29
     * @param \Closure $next
30
     *
31
     * @return mixed
32
     */
33
    public function handle($request, Closure $next)
34
    {
35
        try {
36
            $response = $next($request);
37
            // Was an exception thrown? If so and available catch in our middleware
38
            if (isset($response->exception) && $response->exception) {
39
                throw $response->exception;
40
            }
41
42
            return $response;
43
        } catch (OAuthException $e) {
44
            $data = [
45
                'error' => $e->errorType,
46
                'error_description' => $e->getMessage(),
47
            ];
48
49
            $headers = [];
50
            foreach ($e->getHttpHeaders() as $v) {
51
                $parts = explode(':', $v);
52
                if (count($parts) > 1) {
53
                    $headers[$parts[0]] = $parts[1];
54
                }
55
            }
56
57
            return new JsonResponse($data, $e->httpStatusCode, $headers);
58
        }
59
    }
60
}
61