Completed
Pull Request — master (#376)
by David
03:51
created

PurgeListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 84
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSubscribedEvents() 0 6 1
A getOptionsResolver() 0 8 1
B handlePurge() 0 23 4
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 Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
/**
18
 * Purge handler for the symfony built-in HttpCache.
19
 *
20
 * @author David Buchmann <[email protected]>
21
 *
22
 * {@inheritdoc}
23
 */
24
class PurgeListener extends AccessControlledListener
25
{
26
    const DEFAULT_PURGE_METHOD = 'PURGE';
27
28
    /**
29
     * The purge method to use.
30
     *
31
     * @var string
32
     */
33
    private $purgeMethod;
34
35
    /**
36
     * When creating the purge listener, you can configure an additional option.
37
     *
38
     * - purge_method: HTTP method that identifies purge requests.
39
     *
40
     * @param array $options Options to overwrite the default options
41
     *
42
     * @throws \InvalidArgumentException if unknown keys are found in $options
43
     *
44
     * @see AccessControlledListener::__construct
45
     */
46 8
    public function __construct(array $options = [])
47
    {
48 8
        parent::__construct($options);
49
50 6
        $this->purgeMethod = $this->getOptionsResolver()->resolve($options)['purge_method'];
51 6
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public static function getSubscribedEvents()
57
    {
58
        return [
59 1
            Events::PRE_INVALIDATE => 'handlePurge',
60
        ];
61
    }
62
63
    /**
64
     * Look at unsafe requests and handle purge requests.
65
     *
66
     * Prevents access when the request comes from a non-authorized client.
67
     *
68
     * @param CacheEvent $event
69
     */
70 5
    public function handlePurge(CacheEvent $event)
71
    {
72 5
        $request = $event->getRequest();
73 5
        if ($this->purgeMethod !== $request->getMethod()) {
74 1
            return;
75
        }
76
77 4
        if (!$this->isRequestAllowed($request)) {
78 2
            $event->setResponse(new Response('', 400));
79
80 2
            return;
81
        }
82
83 2
        $response = new Response();
84 2
        $store = $event->getKernel()->getStore();
85 1
86
        if ($store->purge($request->getUri())) {
87 1
            $response->setStatusCode(200, 'Purged');
88
        } else {
89 2
            $response->setStatusCode(200, 'Not found');
90 2
        }
91
        $event->setResponse($response);
92
    }
93
94
    /**
95
     * Add the purge_method option.
96
     *
97 8
     * @return OptionsResolver
98
     */
99 8
    protected function getOptionsResolver()
100 8
    {
101 8
        $resolver = parent::getOptionsResolver();
102
        $resolver->setDefault('purge_method', static::DEFAULT_PURGE_METHOD);
103 8
        $resolver->setAllowedTypes('purge_method', 'string');
104
105
        return $resolver;
106
    }
107
}
108