RequestContentDecompress::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Softonic\RequestContentDecompress\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\App;
10
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
11
12
class RequestContentDecompress
13
{
14
    /**
15
     * @var App
16
     */
17
    private $app;
18
19 6
    public function __construct(Application $app)
20
    {
21 6
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type object<Illuminate\Contra...Foundation\Application> is incompatible with the declared type object<Illuminate\Support\Facades\App> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22 6
    }
23
24
    /**
25
     * Handle an incoming request.
26
     *
27
     * @param Request  $request
28
     * @param \Closure $next
29
     *
30
     * @return mixed
31
     */
32 6
    public function handle($request, Closure $next)
33
    {
34 6
        $newRequest = $request;
35 6
        if ($request->header('Content-Encoding') === 'gzip') {
36 4
            $zippedContent = $request->getContent();
37 4
            $request->headers->remove('Content-Encoding');
38 4
            $content = @gzdecode($zippedContent);
39 4
            if ($content === false) {
40 2
                return new JsonResponse(['error' => 'Invalid data encoding.'], 415);
41
            }
42 2
            $baseRequest = new SymfonyRequest();
43 2
            $baseRequest->initialize(
44 2
                $request->query->all(),
45 2
                $request->request->all(),
46 2
                $request->attributes->all(),
47 2
                $request->cookies->all(),
48 2
                $request->files->all(),
49 2
                $request->server->all(),
50 2
                $content
51
            );
52 2
            $newRequest = Request::createFromBase($baseRequest);
53 2
            $this->app->instance(Request::class, $newRequest);
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\App. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
        }
55
56 4
        return $next($newRequest);
57
    }
58
}
59