UnalteredPsrHttpFactory::createResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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