Completed
Pull Request — master (#310)
by David
06:25
created

RefreshSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
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\RequestMatcherInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
/**
18
 * Refresh handler for the symfony built-in HttpCache.
19
 *
20
 * To use this handler, make sure that your HttpCache makes the fetch method
21
 * public.
22
 *
23
 * @author David Buchmann <[email protected]>
24
 *
25
 * {@inheritdoc}
26
 */
27
class RefreshSubscriber extends AccessControlledSubscriber
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public static function getSubscribedEvents()
33
    {
34
        return [
35
            Events::PRE_HANDLE => 'handleRefresh',
36
        ];
37
    }
38
39
    /**
40
     * Look at safe requests and handle refresh requests.
41
     *
42
     * Ignore refresh to let normal lookup happen when the request comes from
43
     * a non-authorized client.
44
     *
45
     * @param CacheEvent $event
46
     */
47 5
    public function handleRefresh(CacheEvent $event)
48
    {
49 5
        $request = $event->getRequest();
50 5
        if (!$request->isMethodSafe()
51 5
            || !$request->isNoCache()
52 4
            || !$this->isRequestAllowed($request)
53 5
        ) {
54 4
            return;
55
        }
56
57 1
        $event->setResponse(
58 1
            $event->getKernel()->fetch($request)
59 1
        );
60 1
    }
61
}
62