KernelDispatcher   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 85
ccs 36
cts 40
cp 0.9
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 27 6
A invalidate() 0 33 5
A __construct() 0 3 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 FOS\HttpCache\Exception\ExceptionCollection;
15
use FOS\HttpCache\Exception\ProxyUnreachableException;
16
use FOS\HttpCache\ProxyClient\Dispatcher;
17
use Psr\Http\Message\RequestInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\HttpKernelInterface;
20
21
/**
22
 * This Dispatcher directly calls the Symfony HttpCache kernel without
23
 * executing any actual HTTP requests.
24
 *
25
 * This dispatcher can only be used if your application runs on one single
26
 * server.
27
 *
28
 * If you use Varnish/Nginx or have multiple servers, this client can not be
29
 * used.
30
 *
31
 * @author Yanick Witschi <[email protected]>
32
 */
33
class KernelDispatcher implements Dispatcher
34
{
35
    /**
36
     * @var HttpCacheProvider
37
     */
38
    private $httpCacheProvider;
39
40
    /**
41
     * @var array
42
     */
43
    private $queue = [];
44
45 2
    public function __construct(HttpCacheProvider $httpCacheProvider)
46
    {
47 2
        $this->httpCacheProvider = $httpCacheProvider;
48 2
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function invalidate(RequestInterface $invalidationRequest, $validateHost = true)
54
    {
55 2
        $request = Request::create(
56 2
            $invalidationRequest->getUri(),
57 2
            $invalidationRequest->getMethod(),
58 2
            [],
59 2
            [],
60 2
            [],
61 2
            ['REMOTE_ADDR' => '127.0.0.1'],
62 2
            $invalidationRequest->getBody()->getContents()
63
        );
64
65
        // Add headers
66 2
        $headers = $invalidationRequest->getHeaders();
67 2
        foreach ($headers as $name => $values) {
68 2
            $name = strtolower($name);
69
70
            // symfony won't look at the cookies header but parses it when creating the request
71 2
            if ('cookie' === $name) {
72 2
                foreach ($values as $value) {
73 2
                    foreach (explode(';', $value) as $cookieString) {
74 2
                        $chunks = explode('=', $cookieString, 2);
75 2
                        $request->cookies->add([trim($chunks[0]) => trim($chunks[1])]);
76
                    }
77
                }
78
79 2
                continue;
80
            }
81
82 2
            $request->headers->set($name, $values);
83
        }
84
85 2
        $this->queue[sha1($request)] = $request;
86 2
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function flush()
92
    {
93 2
        if (!count($this->queue)) {
94
            return 0;
95
        }
96 2
        $queue = $this->queue;
97 2
        $this->queue = [];
98
99 2
        $exceptions = new ExceptionCollection();
100 2
        $httpCache = $this->httpCacheProvider->getHttpCache();
101 2
        if (null === $httpCache) {
102 1
            throw new ProxyUnreachableException('Kernel did not return a HttpCache instance. Did you forget $kernel->setHttpCache($cacheKernel) in your front controller?');
103
        }
104
105 1
        foreach ($queue as $request) {
106
            try {
107 1
                $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false);
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

107
                $httpCache->handle($request, /** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST, false);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
108
            } catch (\Exception $e) {
109
                $exceptions->add($e);
110
            }
111
        }
112
113 1
        if (count($exceptions)) {
114
            throw $exceptions;
115
        }
116
117 1
        return count($queue);
118
    }
119
}
120