Completed
Pull Request — master (#419)
by Yanick
06:28
created

KernelClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A sendAsyncRequest() 0 19 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\HttpKernel\HttpKernelInterface;
20
use Zend\Diactoros\ServerRequest;
21
22
class KernelClient implements HttpAsyncClient
23
{
24
    /**
25
     * @var HttpCacheAwareKernelInterface
26
     */
27
    private $kernel;
28
29
    /**
30
     * @var HttpFoundationFactory
31
     */
32
    private $httpFoundationFactory;
33
34
    /**
35
     * @var DiactorosFactory
36
     */
37
    private $psr7Factory;
38
39
    /**
40
     * KernelClient constructor.
41
     *
42
     * @param HttpCacheAwareKernelInterface|null $kernel
43
     */
44
    public function __construct(HttpCacheAwareKernelInterface $kernel = null)
45
    {
46
        $this->kernel = $kernel;
47
48
        if (!class_exists(HttpFoundationFactory::class)) {
49
            throw new \RuntimeException('Install symfony/psr-http-message-bridge to use this client.');
50
        }
51
52
        if (!class_exists(DiactorosFactory::class)) {
53
            throw new \RuntimeException('Install zendframework/zend-diactoros to use this client.');
54
        }
55
56
        $this->httpFoundationFactory = new HttpFoundationFactory();
57
        $this->psr7Factory = new DiactorosFactory();
58
    }
59
60
    /**
61
     * Converts a PSR-7 request to a Symfony HttpFoundation request, runs
62
     * it through the HttpCache kernel and converts the response back to a PSR-7
63
     * response.
64
     *
65
     * {@inheritdoc}
66
     */
67
    public function sendAsyncRequest(RequestInterface $request)
68
    {
69
        $serverRequest = new ServerRequest(
70
            ['REMOTE_ADDR', '127.0.0.1'],
71
            [],
72
            $request->getUri(),
73
            $request->getMethod(),
74
            $request->getBody(),
75
            $request->getHeaders(),
76
            [],
77
            explode('&', $request->getUri()->getQuery())
78
        );
79
80
        $symfonyRequest = $this->httpFoundationFactory->createRequest($serverRequest);
81
        $symfonyResponse = $this->kernel->getHttpCache()->handle($symfonyRequest, HttpKernelInterface::MASTER_REQUEST, false);
82
        $psrResponse = $this->psr7Factory->createResponse($symfonyResponse);
83
84
        return new FulfilledPromise($psrResponse);
85
    }
86
}
87