Completed
Pull Request — master (#290)
by Gavin
30:42 queued 28:01
created

PurgeSubscriber::handlePurge()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 13
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 21
ccs 13
cts 14
cp 0.9286
crap 4.0058
rs 9.0534
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\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestMatcher;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\HttpKernelInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
/**
22
 * Purge handler for the symfony built-in HttpCache.
23
 *
24
 * @author David Buchmann <[email protected]>
25
 *
26
 * {@inheritdoc}
27
 */
28
class PurgeSubscriber extends AccessControlledSubscriber
29
{
30
    const DEFAULT_PURGE_METHOD = 'PURGE';
31
32
    /**
33
     * The options configured in the constructor argument or default values.
34
     *
35
     * @var array
36
     */
37
    private $options = [];
38
39
    /**
40
     * When creating this subscriber, you can configure a number of options.
41
     *
42
     * - purge_method:         HTTP method that identifies purge requests.
43
     * - purge_client_matcher: RequestMatcher to identify valid purge clients.
44
     * - purge_client_ips:     IP or array of IPs that are allowed to purge.
45
     *
46
     * Only set one of purge_client_ips and purge_client_matcher.
47
     *
48
     * @param array $options Options to overwrite the default options
49
     *
50
     * @throws \InvalidArgumentException if unknown keys are found in $options
51
     */
52 5
    public function __construct(array $options = [])
53
    {
54 5
        $resolver = new OptionsResolver();
55 5
        if (method_exists($resolver, 'setDefined')) {
56
            // this was only added in symfony 2.6
57 5
            $resolver->setDefined(['purge_client_matcher', 'purge_client_ips', 'purge_method']);
58 5
        } else {
59
            $resolver->setOptional(['purge_client_matcher', 'purge_client_ips', 'purge_method']);
0 ignored issues
show
Bug introduced by
The method setOptional() does not seem to exist on object<Symfony\Component...solver\OptionsResolver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        }
61 5
        $resolver->setDefaults([
62 5
            'purge_client_matcher' => null,
63 5
            'purge_client_ips' => null,
64 5
            'purge_method' => static::DEFAULT_PURGE_METHOD,
65 5
        ]);
66
67 5
        $this->options = $resolver->resolve($options);
68
69 4
        parent::__construct($this->options['purge_client_matcher'], $this->options['purge_client_ips']);
70 4
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public static function getSubscribedEvents()
76
    {
77
        return [
78
            Events::PRE_INVALIDATE => 'handlePurge',
79
        ];
80
    }
81
82
    /**
83
     * Look at unsafe requests and handle purge requests.
84
     *
85
     * Prevents access when the request comes from a non-authorized client.
86
     *
87
     * @param CacheEvent $event
88
     */
89 4
    public function handlePurge(CacheEvent $event)
90
    {
91 4
        $request = $event->getRequest();
92 4
        if ($this->options['purge_method'] !== $request->getMethod()) {
93
            return;
94
        }
95
96 4
        if (!$this->isRequestAllowed($request)) {
97 2
            $event->setResponse(new Response('', 400));
98
99 2
            return;
100
        }
101
102 2
        $response = new Response();
103 2
        if ($event->getKernel()->getStore()->purge($request->getUri())) {
104 1
            $response->setStatusCode(200, 'Purged');
105 1
        } else {
106 1
            $response->setStatusCode(200, 'Not found');
107
        }
108 2
        $event->setResponse($response);
109 2
    }
110
}
111