Completed
Pull Request — master (#746)
by
unknown
83:49 queued 18:51
created

OAuthExceptionHandlerMiddleware::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.2
cc 4
eloc 11
nc 3
nop 2
crap 20
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
            return new JsonResponse($data, $e->httpStatusCode, $e->getHttpHeaders());
50
        }
51
    }
52
}
53