DebugListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A handleDebug() 0 12 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\EventDispatcher\EventSubscriberInterface;
15
16
/**
17
 * Debug handler for the symfony built-in HttpCache.
18
 *
19
 * Add debug information to the response for use in cache tests.
20
 *
21
 * @author David Buchmann <[email protected]>
22
 *
23
 * {@inheritdoc}
24
 */
25
class DebugListener implements EventSubscriberInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public static function getSubscribedEvents(): array
31
    {
32
        return [
33 1
            Events::POST_HANDLE => 'handleDebug',
34
        ];
35
    }
36
37
    /**
38
     * Extract the cache HIT/MISS information from the X-Symfony-Cache header.
39
     *
40
     * For this header to be present, the HttpCache must be created with the
41
     * debug option set to true.
42
     */
43 5
    public function handleDebug(CacheEvent $event)
44
    {
45 5
        $response = $event->getResponse();
46 5
        if ($response->headers->has('X-Symfony-Cache')) {
47 3
            if (false !== strpos($response->headers->get('X-Symfony-Cache'), 'miss')) {
48 1
                $state = 'MISS';
49 2
            } elseif (false !== strpos($response->headers->get('X-Symfony-Cache'), 'fresh')) {
50 1
                $state = 'HIT';
51
            } else {
52 1
                $state = 'UNDETERMINED';
53
            }
54 3
            $response->headers->set('X-Cache', $state);
55
        }
56 5
    }
57
}
58