Completed
Pull Request — master (#736)
by
unknown
62:36
created

OAuthExceptionHandlerMiddleware::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 17
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 12
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.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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...
38
            if (isset($response->exception) && $response->exception instanceof OAuthException) {
39
40
                $data = [
41
                    'error' => $response->exception->errorType,
42
                    'error_description' => $response->exception->getMessage(),
43
                ];
44
45
                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 125 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...
46
            }
47
48
            return $response;
49
    }
50
}
51