Completed
Pull Request — master (#392)
by David
02:46
created

RefreshListener::handleRefresh()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

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