Completed
Pull Request — master (#437)
by Yanick
06:31 queued 02:28
created

Symfony::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\ProxyClient\Invalidation\ClearCapable;
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\PurgeListener;
19
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
20
21
/**
22
 * Symfony HttpCache invalidator.
23
 *
24
 * Additional constructor options:
25
 * - purge_method:         HTTP method that identifies purge requests.
26
 *
27
 * @author David de Boer <[email protected]>
28
 * @author David Buchmann <[email protected]>
29
 */
30
class Symfony extends HttpProxyClient implements PurgeCapable, RefreshCapable, TagCapable, ClearCapable
31
{
32
    const HTTP_METHOD_REFRESH = 'GET';
33
34 4
    /**
35
     * {@inheritdoc}
36 4
     */
37
    public function purge($url, array $headers = [])
38 4
    {
39
        $this->queueRequest($this->options['purge_method'], $url, $headers);
40
41
        return $this;
42
    }
43
44 3
    /**
45
     * {@inheritdoc}
46 3
     */
47 3 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...
48
    {
49 3
        $headers = array_merge($headers, ['Cache-Control' => 'no-cache']);
50
        $this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers);
51
52 7
        return $this;
53
    }
54 7
55 7
    protected function configureOptions()
56 7
    {
57
        $resolver = parent::configureOptions();
58
        $resolver->setDefaults([
59 7
            'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD,
60
            'clear_cache_header' => PurgeListener::DEFAULT_CLEAR_CACHE_HEADER,
61
            'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD,
62
            'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER,
63
            'tags_invalidate_path' => '/',
64
            'header_length' => 7500,
65
        ]);
66
        $resolver->setAllowedTypes('purge_method', 'string');
67
        $resolver->setAllowedTypes('clear_cache_header', 'string');
68
        $resolver->setAllowedTypes('tags_method', 'string');
69
        $resolver->setAllowedTypes('tags_header', 'string');
70
        $resolver->setAllowedTypes('tags_invalidate_path', 'string');
71
        $resolver->setAllowedTypes('header_length', 'int');
72
73
        return $resolver;
74
    }
75
76
    /**
77
     * Remove/Expire cache objects based on cache tags.
78
     *
79
     * @param array $tags Tags that should be removed/expired from the cache
80
     *
81
     * @return $this
82
     */
83
    public function invalidateTags(array $tags)
84
    {
85
        $escapedTags = $this->escapeTags($tags);
86
87
        $chunkSize = $this->determineTagsPerHeader($escapedTags, ',');
88
89
        foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) {
90
            $this->queueRequest(
91
                $this->options['tags_method'],
92
                $this->options['tags_invalidate_path'],
93
                [$this->options['tags_header'] => implode(',', $tagchunk)],
94
                false
95
            );
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Clear the cache completely.
103
     *
104
     * @return $this
105
     */
106
    public function clear()
107
    {
108
        $this->queueRequest(
109
            $this->options['purge_method'], '/',
110
            [$this->options['clear_cache_header'] => 'true'],
111
            false
112
        );
113
    }
114
}
115