ExpectJsonOrThrow::throwable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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