Completed
Pull Request — master (#373)
by Yanick
02:46
created

PurgeListener::handlePurge()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 20
cts 20
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 6
nop 1
crap 7
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\ProxyClient\Symfony;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
/**
19
 * Purge handler for the symfony built-in HttpCache.
20
 *
21
 * @author David Buchmann <[email protected]>
22
 * @author Yanick Witschi <[email protected]>
23
 *
24
 * {@inheritdoc}
25
 */
26
class PurgeListener extends AccessControlledListener
27
{
28
    const DEFAULT_PURGE_METHOD = 'PURGE';
29
    const DEFAULT_PURGE_TAGS_HEADER = 'X-Cache-Tags';
30
31
    /**
32
     * The purge method to use.
33
     *
34
     * @var string
35
     */
36
    private $purgeMethod;
37
38
    /**
39
     * The purge tags header to use.
40
     *
41
     * @var string
42
     */
43
    private $purgeTagsHeader;
44
45
    /**
46
     * When creating the purge listener, you can configure an additional option.
47
     *
48
     * - purge_method: HTTP method that identifies purge requests.
49
     * - purge_tags_header: HTTP header that contains cache tags to invalidate.
50
     *
51
     * @param array $options Options to overwrite the default options
52
     *
53
     * @throws \InvalidArgumentException if unknown keys are found in $options
54
     *
55
     * @see AccessControlledListener::__construct
56
     */
57 9
    public function __construct(array $options = [])
58
    {
59 9
        parent::__construct($options);
60
61 7
        $this->purgeMethod = $this->getOptionsResolver()->resolve($options)['purge_method'];
62 7
        $this->purgeTagsHeader = $this->getOptionsResolver()->resolve($options)['purge_tags_header'];
63 7
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public static function getSubscribedEvents()
69
    {
70
        return [
71 1
            Events::PRE_INVALIDATE => 'handlePurge',
72
        ];
73
    }
74
75
    /**
76
     * Look at unsafe requests and handle purge requests.
77
     *
78
     * Prevents access when the request comes from a non-authorized client.
79
     *
80
     * @param CacheEvent $event
81
     */
82 6
    public function handlePurge(CacheEvent $event)
83
    {
84 6
        $request = $event->getRequest();
85 6
        if ($this->purgeMethod !== $request->getMethod()) {
86 1
            return;
87
        }
88
89 5
        if (!$this->isRequestAllowed($request)) {
90 2
            $event->setResponse(new Response('', 400));
91
92 2
            return;
93
        }
94
95 3
        $response = new Response();
96 3
        $store = $event->getKernel()->getStore();
97
98 3
        if ($request->headers->has($this->purgeTagsHeader)
99 3
            && $store instanceof TaggableStore
100
        ) {
101
            // TODO: need to unescape again here
102 1
            $tags = explode(',', $request->headers->get($this->purgeTagsHeader));
103
104 1
            if ($store->invalidateTags($tags)) {
105 1
                $response->setStatusCode(200, 'Purged');
106
            } else {
107 1
                $response->setStatusCode(200, 'Not found');
108
            }
109 2
        } elseif ($store->purge($request->getUri())) {
110 1
            $response->setStatusCode(200, 'Purged');
111
        } else {
112 1
            $response->setStatusCode(200, 'Not found');
113
        }
114 3
        $event->setResponse($response);
115 3
    }
116
117
    /**
118
     * Add the purge_method option.
119
     *
120
     * @return OptionsResolver
121
     */
122 9 View Code Duplication
    protected function getOptionsResolver()
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...
123
    {
124 9
        $resolver = parent::getOptionsResolver();
125 9
        $resolver->setDefault('purge_method', static::DEFAULT_PURGE_METHOD);
126 9
        $resolver->setAllowedTypes('purge_method', 'string');
127 9
        $resolver->setDefault('purge_tags_header', static::DEFAULT_PURGE_TAGS_HEADER);
128 9
        $resolver->setAllowedTypes('purge_tags_header', 'string');
129
130 9
        return $resolver;
131
    }
132
}
133