Completed
Pull Request — master (#736)
by
unknown
09:44
created

OAuthExceptionHandlerMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 3
c 6
b 2
f 2
lcom 0
cbo 2
dl 0
loc 27
ccs 0
cts 8
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 16 3
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
        $response = $next($request);
36
37
        // Was an OAuthException previously caught by the pipeline? If so, hijack response, replacing with json error.
38
        if (isset($response->exception) && $response->exception instanceof OAuthException) {
39
            $data = [
40
                'error' => $response->exception->errorType,
41
                'error_description' => $response->exception->getMessage(),
42
            ];
43
44
            return new JsonResponse($data, $response->exception->httpStatusCode, $response->exception->getHttpHeaders());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
45
        }
46
47
        return $response;
48
    }
49
}
50