NextHandlerAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 78
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handle() 0 7 1
A convertRequest() 0 14 1
A getPsr7Response() 0 4 1
1
<?php
2
3
namespace Softonic\Laravel\Middleware\Psr15Bridge;
4
5
use Closure;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
10
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class NextHandlerAdapter implements RequestHandlerInterface
14
{
15
    /**
16
     * @var Request
17
     */
18
    private $foundationRequest;
19
20
    /**
21
     * @var Closure
22
     */
23
    private $next;
24
25
    /**
26
     * @var PsrHttpFactory
27
     */
28
    private $psrHttpFactory;
29
30
    /**
31
     * @var HttpFoundationFactory
32
     */
33
    private $httpFoundationFactory;
34
35 2
    public function __construct(
36
        HttpFoundationFactory $httpFoundationFactory,
37
        PsrHttpFactory $psrHttpFactory,
38
        Request $foundationRequest,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $foundationRequest. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
39
        Closure $next
40
    ) {
41 2
        $this->psrHttpFactory      = $psrHttpFactory;
42 2
        $this->foundationRequest     = $foundationRequest;
43 2
        $this->next                  = $next;
44 2
        $this->httpFoundationFactory = $httpFoundationFactory;
45 2
    }
46
47
    /**
48
     * Intercept communication between the PSR-15 middleware and the next middleware/controller.
49
     *
50
     * To allow the next execution we need to restore the request to a HttpFoundationRequest
51
     * and wait for a response that must be adapter to PSR-7 response to allow the
52
     * PSR-15 middleware process it.
53
     *
54
     * @param ServerRequestInterface $psr7Request
55
     *
56
     * @return ResponseInterface
57
     */
58 2
    public function handle(ServerRequestInterface $psr7Request): ResponseInterface
59
    {
60 2
        $request  = $this->convertRequest($psr7Request, $this->foundationRequest);
61 2
        $response = ($this->next)($request);
62
63 2
        return $this->getPsr7Response($response);
64
    }
65
66 2
    private function convertRequest(ServerRequestInterface $psr7Request, $originalRequest): Request
67
    {
68 2
        $foundation_request = $this->httpFoundationFactory->createRequest($psr7Request);
69
70 2
        $originalRequest->query      = clone $foundation_request->query;
71 2
        $originalRequest->request    = clone $foundation_request->request;
72 2
        $originalRequest->attributes = clone $foundation_request->attributes;
73 2
        $originalRequest->cookies    = clone $foundation_request->cookies;
74 2
        $originalRequest->files      = clone $foundation_request->files;
75 2
        $originalRequest->server     = clone $foundation_request->server;
76 2
        $originalRequest->headers    = clone $foundation_request->headers;
77
78 2
        return $originalRequest;
79
    }
80
81
    /**
82
     * @param $response
83
     *
84
     * @return ResponseInterface
85
     */
86 2
    protected function getPsr7Response($response): ResponseInterface
87
    {
88 2
        return $this->psrHttpFactory->createResponse($response);
89
    }
90
}
91