Passed
Push — master ( 17b580...ced9bf )
by Darko
06:25
created

ForceJsonOnAPI   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
eloc 4
c 2
b 1
f 0
dl 0
loc 15
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 8 3
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class ForceJsonOnAPI
11
{
12
    /**
13
     * Handle an incoming request.
14
     *
15
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
16
     */
17
    public function handle(Request $request, Closure $next): Response
18
    {
19
        // Force Json accept type on api routes
20
        if ($request->is('api/*') && ! Str::contains($request->header('accept'), ['/json', '+json'])) {
0 ignored issues
show
Bug introduced by
It seems like $request->header('accept') can also be of type array; however, parameter $haystack of Illuminate\Support\Str::contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        if ($request->is('api/*') && ! Str::contains(/** @scrutinizer ignore-type */ $request->header('accept'), ['/json', '+json'])) {
Loading history...
21
            $request->headers->set('accept', 'application/json,'.$request->header('accept'));
0 ignored issues
show
Bug introduced by
Are you sure $request->header('accept') of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
            $request->headers->set('accept', 'application/json,'./** @scrutinizer ignore-type */ $request->header('accept'));
Loading history...
22
        }
23
24
        return $next($request);
25
    }
26
}
27