Completed
Pull Request — master (#419)
by Yanick
03:07
created

KernelClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

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