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

ExpectJsonOrThrow   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 6
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A throwable() 0 3 1
A handle() 0 7 2
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