Completed
Pull Request — master (#419)
by Yanick
13:42 queued 11:43
created

Symfony   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 154
Duplicated Lines 4.55 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 7
loc 154
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A purge() 0 6 1
A refresh() 7 7 1
A isDirectRoutingEnabled() 0 10 3
A configureOptions() 0 18 1
B flush() 0 23 5
A invalidateTags() 0 17 2
A queueRequest() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ProxyClient;
13
14
use FOS\HttpCache\Exception\ExceptionCollection;
15
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
16
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
17
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
18
use FOS\HttpCache\SymfonyCache\HttpCacheAwareKernelInterface;
19
use FOS\HttpCache\SymfonyCache\PurgeListener;
20
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
21
use Http\Message\RequestFactory;
22
use Symfony\Component\HttpFoundation\Request;
23
24
/**
25
 * Symfony HttpCache invalidator.
26
 *
27
 * Additional constructor options:
28
 * - purge_method:         HTTP method that identifies purge requests.
29
 *
30
 * @author David de Boer <[email protected]>
31
 * @author David Buchmann <[email protected]>
32
 */
33
class Symfony extends HttpProxyClient implements PurgeCapable, RefreshCapable, TagCapable
34 4
{
35
    const HTTP_METHOD_REFRESH = 'GET';
36 4
37
    /**
38 4
     * @var array
39
     */
40
    private $queue;
41
42
    /**
43
     * @var HttpCacheAwareKernelInterface
44 3
     */
45
    private $kernel;
46 3
47 3
    /**
48
     * Additional parameter for (optional) kernel.
49 3
     *
50
     * {@inheritdoc}
51
     */
52 7
    public function __construct(
53
        HttpDispatcher $httpDispatcher,
54 7
        array $options = [],
55 7
        RequestFactory $messageFactory = null,
56 7
        HttpCacheAwareKernelInterface $kernel = null
57
    ) {
58
        parent::__construct($httpDispatcher, $options, $messageFactory);
59 7
60
        $this->kernel = $kernel;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function purge($url, array $headers = [])
67
    {
68
        $this->queueRequest($this->options['purge_method'], $url, $headers);
69
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function refresh($url, array $headers = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $headers = array_merge($headers, ['Cache-Control' => 'no-cache']);
79
        $this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers);
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function configureOptions()
88
    {
89
        $resolver = parent::configureOptions();
90
        $resolver->setDefaults([
91
            'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD,
92
            'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD,
93
            'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER,
94
            'header_length' => 7500,
95
            'enable_kernel_routing' => false,
96
        ]);
97
        $resolver->setAllowedTypes('purge_method', 'string');
98
        $resolver->setAllowedTypes('tags_method', 'string');
99
        $resolver->setAllowedTypes('tags_header', 'string');
100
        $resolver->setAllowedTypes('header_length', 'int');
101
        $resolver->setAllowedTypes('enable_kernel_routing', 'boolean');
102
103
        return $resolver;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function flush()
110
    {
111
        if (!$this->isDirectRoutingEnabled()) {
112
            return parent::flush();
113
        }
114
115
        $exceptions = new ExceptionCollection();
116
117
        foreach ($this->queue as $request) {
118
            try {
119
                $this->kernel->getHttpCache()
120
                    ->handle($request, HttpCacheAwareKernelInterface::MASTER_REQUEST);
121
            } catch (\Exception $e) {
122
                $exceptions->add($e);
123
            }
124
        }
125
126
        if (count($exceptions)) {
127
            throw $exceptions;
128
        }
129
130
        return count($this->queue);
131
    }
132
133
    /**
134
     * Remove/Expire cache objects based on cache tags.
135
     *
136
     * @param array $tags Tags that should be removed/expired from the cache
137
     *
138
     * @return $this
139
     */
140
    public function invalidateTags(array $tags)
141
    {
142
        $escapedTags = $this->escapeTags($tags);
143
144
        $chunkSize = $this->determineTagsPerHeader($escapedTags, ',');
145
146
        foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) {
147
            $this->queueRequest(
148
                $this->options['tags_method'],
149
                '/',
150
                [$this->options['tags_header'] => implode(',', $tagchunk)],
151
                false
152
            );
153
        }
154
155
        return $this;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    protected function queueRequest($method, $url, array $headers, $validateHost = true)
162
    {
163
        if (!$this->isDirectRoutingEnabled()) {
164
            return parent::queueRequest($method, $url, $headers, $validateHost);
165
        }
166
167
        $request = Request::create((string) $url, $method);
168
        $request->headers->replace($headers);
169
170
        $this->queue[sha1((string) $request)] = $request;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    protected function isDirectRoutingEnabled()
177
    {
178
        if ($this->options['enable_kernel_routing']
179
            && $this->kernel instanceof HttpCacheAwareKernelInterface
180
        ) {
181
            return true;
182
        }
183
184
        return false;
185
    }
186
}
187