Passed
Push — master ( d0507a...933939 )
by Andrea Marco
02:43 queued 15s
created

ExpectJsonOrThrow::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\JsonApiError\Middleware;
6
7
use Closure;
8
use Illuminate\Http\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Throwable;
12
13
/**
14
 * The middleware to ensure that the request expects a JSON response.
15
 */
16
class ExpectJsonOrThrow
17
{
18
    /**
19
     * Handle an incoming request.
20
     *
21
     * @param Closure(Request): Response $next
22
     */
23
    public function handle(Request $request, Closure $next): Response
24
    {
25
        if ($request->expectsJson()) {
26
            return $next($request);
27
        }
28
29
        throw $this->throwable();
30
    }
31
32
    /**
33
     * Retrieve the Throwable instance to throw.
34
     */
35
    protected function throwable(): Throwable
36
    {
37
        return new NotFoundHttpException();
38
    }
39
}
40