Completed
Pull Request — master (#419)
by Yanick
04:38
created

KernelClient::sendAsyncRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\SymfonyCache;
13
14
use Http\Client\HttpAsyncClient;
15
use Http\Promise\FulfilledPromise;
16
use Psr\Http\Message\RequestInterface;
17
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
18
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Component\HttpKernel\HttpKernelInterface;
21
use Zend\Diactoros\ServerRequest;
22
23
class KernelClient implements HttpAsyncClient
24
{
25
    /**
26
     * @var RequestStack
27
     */
28
    private $requestStack;
29
30
    /**
31
     * @var HttpCacheAwareKernelInterface
32
     */
33
    private $kernel;
34
35
    /**
36
     * @var HttpFoundationFactory
37
     */
38
    private $httpFoundationFactory;
39
40
    /**
41
     * @var DiactorosFactory
42
     */
43
    private $psr7Factory;
44
45
    /**
46
     * KernelClient constructor.
47
     *
48
     * @param RequestStack                       $requestStack
49
     * @param HttpCacheAwareKernelInterface|null $kernel
50
     */
51
    public function __construct(RequestStack $requestStack, HttpCacheAwareKernelInterface $kernel = null)
52
    {
53
        $this->requestStack = $requestStack;
54
        $this->kernel = $kernel;
55
56
        if (!class_exists(HttpFoundationFactory::class)) {
57
            throw new \RuntimeException('Install symfony/psr-http-message-bridge to use this client.');
58
        }
59
60
        if (!class_exists(DiactorosFactory::class)) {
61
            throw new \RuntimeException('Install zendframework/zend-diactoros to use this client.');
62
        }
63
64
        $this->httpFoundationFactory = new HttpFoundationFactory();
65
        $this->psr7Factory = new DiactorosFactory();
66
        $this->requestStack = $requestStack;
67
    }
68
69
    /**
70
     * Converts a PSR-7 request to a Symfony HttpFoundation request, runs
71
     * it through the HttpCache kernel and converts the response back to a PSR-7
72
     * response.
73
     *
74
     * {@inheritdoc}
75
     */
76
    public function sendAsyncRequest(RequestInterface $request)
77
    {
78
        $currentRequest = $this->requestStack->getCurrentRequest();
79
80
        $serverRequest = new ServerRequest(
81
            $currentRequest->server->all(),
82
            [],
83
            $request->getUri(),
84
            $request->getMethod(),
85
            $request->getBody(),
86
            $request->getHeaders(),
87
            [],
88
            explode('&', $request->getUri()->getQuery())
89
        );
90
91
        $symfonyRequest = $this->httpFoundationFactory->createRequest($serverRequest);
92
        $symfonyResponse = $this->kernel->getHttpCache()->handle($symfonyRequest, HttpKernelInterface::MASTER_REQUEST, false);
93
        $psrResponse = $this->psr7Factory->createResponse($symfonyResponse);
94
95
        return new FulfilledPromise($psrResponse);
96
    }
97
}
98