Completed
Pull Request — master (#437)
by Yanick
11:28
created

Symfony::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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