PreventRedirect::handle()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 10
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Loadsman\LaravelPlugin\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class PreventRedirect
9
{
10
    const CATCH_REDIRECT_HEADER = 'catch-redirect';
11
12
    public function handle(Request $request, Closure $next)
13
    {
14
        /**
15
         * @var \Symfony\Component\HttpFoundation\Response $response
16
         */
17
        $response = $next($request);
18
19
        // In case the request was sent by Api Tester
20
        // and catching request is wanted
21
        // we will halt the response and output redirect information instead.
22
        if ($request->header('X-Api-Tester') === static::CATCH_REDIRECT_HEADER) {
23
            if ($response->isRedirection()) {
24
                /**
25
                 * @var \Symfony\Component\HttpFoundation\RedirectResponse $response
26
                 */
27
                return response()->json(['data' => [
28
                    'location' => $response->getTargetUrl(),
29
                    'status' => $response->getStatusCode(),
30
                ]])->header('X-Api-Tester', 'redirect');
31
            }
32
        }
33
34
        return $response;
35
    }
36
37
}