Passed
Push — master ( 248fa3...de2a8a )
by Pol
14:55
created

UnalteredPsrHttpFactory::parseStr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 9.7
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\UnalteredPsrHttpMessageBridgeBundle\Factory;
6
7
use League\Uri\Parser\QueryString;
8
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
final class UnalteredPsrHttpFactory implements HttpMessageFactoryInterface
13
{
14
    /**
15
     * @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
16
     */
17
    private $httpMessageFactory;
18
19 5
    public function __construct(HttpMessageFactoryInterface $httpMessageFactory)
20
    {
21 5
        $this->httpMessageFactory = $httpMessageFactory;
22 5
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 3
    public function createRequest(Request $symfonyRequest)
28
    {
29
        // Call the original object to avoid duplicating code.
30 3
        $request = $this->httpMessageFactory->createRequest($symfonyRequest);
31
32
        // If a query string does not exist, return the original object.
33 3
        if ('' === $unalteredQueryString = $symfonyRequest->server->get('QUERY_STRING', '')) {
34 1
            return $request;
35
        }
36
37
        // This is where all is happening.
38
        // We do not rely on $symfonyRequest->query->all() because it relies
39
        // on parse_str() which is altering the query string parameters.
40
        // We rely on $symfonyRequest->server->get('QUERY_STRING') so we are
41
        // sure that the query string hasn't been altered.
42
        // Create a new request with the URI and Params updated.
43
        return $request
44 2
            ->withQueryParams(
45 2
                QueryString::extract($unalteredQueryString)
46
            )
47 2
            ->withUri(
48
                $request
49 2
                    ->getUri()
50 2
                    ->withQuery($unalteredQueryString)
51
            );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function createResponse(Response $symfonyResponse)
58
    {
59 1
        return $this->httpMessageFactory->createResponse($symfonyResponse);
60
    }
61
}
62