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

PurgeListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 9.35 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 10
loc 107
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscribedEvents() 0 6 1
C handlePurge() 0 34 7
A getOptionsResolver() 10 10 1

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\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 8
     * When creating the purge listener, you can configure an additional option.
47
     *
48 8
     * - purge_method: HTTP method that identifies purge requests.
49
     * - purge_tags_header: HTTP header that contains cache tags to invalidate.
50 6
     *
51 6
     * @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 1
     */
57
    public function __construct(array $options = [])
58
    {
59 1
        parent::__construct($options);
60 1
61
        $this->purgeMethod = $this->getOptionsResolver()->resolve($options)['purge_method'];
62
        $this->purgeTagsHeader = $this->getOptionsResolver()->resolve($options)['purge_tags_header'];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function getSubscribedEvents()
69
    {
70 5
        return [
71
            Events::PRE_INVALIDATE => 'handlePurge',
72 5
        ];
73 5
    }
74 1
75
    /**
76
     * Look at unsafe requests and handle purge requests.
77 4
     *
78 2
     * Prevents access when the request comes from a non-authorized client.
79
     *
80 2
     * @param CacheEvent $event
81
     */
82
    public function handlePurge(CacheEvent $event)
83 2
    {
84 2
        $request = $event->getRequest();
85 1
        if ($this->purgeMethod !== $request->getMethod()) {
86 1
            return;
87 1
        }
88
89 2
        if (!$this->isRequestAllowed($request)) {
90 2
            $event->setResponse(new Response('', 400));
91
92
            return;
93
        }
94
95
        $response = new Response();
96
        $store = $event->getKernel()->getStore();
97 8
98
        if ($request->headers->has($this->purgeTagsHeader)
99 8
            && $store instanceof TaggableStore
100 8
        ) {
101 8
            // TODO: need to unescape again here
102
            $tags = explode(',', $request->headers->get($this->purgeTagsHeader));
103 8
104
            if ($store->invalidateTags($tags)) {
105
                $response->setStatusCode(200, 'Purged');
106
            } else {
107
                $response->setStatusCode(200, 'Not found');
108
            }
109
        } elseif ($store->purge($request->getUri())) {
110
            $response->setStatusCode(200, 'Purged');
111
        } else {
112
            $response->setStatusCode(200, 'Not found');
113
        }
114
        $event->setResponse($response);
115
    }
116
117
    /**
118
     * Add the purge_method option.
119
     *
120
     * @return OptionsResolver
121
     */
122 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
        $resolver = parent::getOptionsResolver();
125
        $resolver->setDefault('purge_method', static::DEFAULT_PURGE_METHOD);
126
        $resolver->setAllowedTypes('purge_method', 'string');
127
        $resolver->setDefault('purge_tags_header', static::DEFAULT_PURGE_TAGS_HEADER);
128
        $resolver->setAllowedTypes('purge_tags_header', 'string');
129
130
        return $resolver;
131
    }
132
}
133