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

KernelDispatcher::flush()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 8.7972
cc 4
eloc 13
nc 6
nop 0
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 FOS\HttpCache\Exception\ExceptionCollection;
15
use FOS\HttpCache\ProxyClient\Dispatcher;
16
use Psr\Http\Message\RequestInterface;
17
use Symfony\Component\HttpFoundation\Cookie;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\HttpKernelInterface;
20
21
/**
22
 * An implementation of Dispatcher that allows direct calling of the
23
 * Symfony HttpCache kernel without executing a real HTTP request.
24
 * It can only be used if you have a single node setup of Symfony and serves
25
 * as kind of a shortcut for easier configuration.
26
 * If you use Varnish or have a multiple node Symfony setup, this client is entirely
27
 * useless and cannot be used.
28
 *
29
 * @author Yanick Witschi <[email protected]>
30
 */
31
class KernelDispatcher implements Dispatcher
32
{
33
    /**
34
     * @var HttpCacheAwareKernelInterface
35
     */
36
    private $kernel;
37
38
    /**
39
     * @var array
40
     */
41
    private $queue = [];
42
43
    /**
44
     * KernelClient constructor.
45
     *
46
     * @param HttpCacheAwareKernelInterface $kernel
47
     */
48
    public function __construct(HttpCacheAwareKernelInterface $kernel)
49
    {
50
        $this->kernel = $kernel;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function invalidate(RequestInterface $invalidationRequest, $validateHost = true)
57
    {
58
        $request = Request::create(
59
            $invalidationRequest->getUri(),
60
            $invalidationRequest->getMethod(),
61
            [],
62
            [],
63
            [],
64
            ['REMOTE_ADDR' => '127.0.0.1'],
65
            $invalidationRequest->getBody()->getContents()
66
        );
67
68
        // Add headers
69
        $headers = $invalidationRequest->getHeaders();
70
        foreach ($headers as $name => $values) {
71
            $name = strtolower($name);
72
73
            if ('cookie' === $name) {
74
                foreach ($values as $value) {
75
                    foreach (explode(';', $value) as $cookieString) {
76
                        $cookie = Cookie::fromString($cookieString);
77
                        $request->cookies->add([$cookie->getName() => $cookie->getValue()]);
78
                    }
79
                }
80
81
                continue;
82
            }
83
84
            $request->headers->set($name, $values);
85
        }
86
87
88
        $this->queue[sha1($request)] = $request;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function flush()
95
    {
96
        $queue = $this->queue;
97
        $this->queue = [];
98
99
        $exceptions = new ExceptionCollection();
100
101
        $httpCache = $this->kernel->getHttpCache();
102
103
        foreach ($queue as $request) {
104
            try {
105
                $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false);
106
            } catch (\Exception $e) {
107
                $exceptions->add($e);
108
            }
109
        }
110
111
        if (count($exceptions)) {
112
            throw $exceptions;
113
        }
114
115
        return count($queue);
116
    }
117
}
118