Completed
Push — master ( 1c96c0...849954 )
by David
10s
created

SessionListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 56
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onKernelRequest() 0 4 1
A onKernelResponse() 0 16 3
A getSubscribedEvents() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\EventListener;
13
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use Symfony\Component\HttpKernel\EventListener\SessionListener as BaseSessionListener;
18
19
/**
20
 * Decorates the default Symfony session listener.
21
 *
22
 * Since Symfony 3.4, the default Symfony session listener automatically
23
 * overwrites the Cache-Control headers to `private` in case the session has
24
 * been started. This destroys the user context feature of FOSHttpCache.
25
 * Since Symfony 4.1, there is a header we can set to skip this behaviour. We
26
 * set that header in UserContextListener.
27
 * For Symfony 3.4 and 4.0, we decorate the listener to only call the default
28
 * behaviour if `Vary` contains neither the context hash header nor any of the
29
 * user identifier headers, to avoid impacts on other parts of your application.
30
 *
31
 * @author Yanick Witschi <[email protected]>
32
 */
33
final class SessionListener implements EventSubscriberInterface
34
{
35
    /**
36
     * @var BaseSessionListener
37
     */
38
    private $inner;
39
40
    /**
41
     * @var string
42
     */
43
    private $userHashHeader;
44
45
    /**
46
     * @var array
47
     */
48
    private $userIdentifierHeaders;
49
50
    /**
51
     * @param BaseSessionListener $inner
52
     * @param string              $userHashHeader        Must be lower-cased
53
     * @param array               $userIdentifierHeaders Must be lower-cased
54
     */
55 6
    public function __construct(BaseSessionListener $inner, string $userHashHeader, array $userIdentifierHeaders)
56
    {
57 6
        $this->inner = $inner;
58 6
        $this->userHashHeader = $userHashHeader;
59 6
        $this->userIdentifierHeaders = $userIdentifierHeaders;
60 6
    }
61
62 1
    public function onKernelRequest(GetResponseEvent $event)
63
    {
64 1
        return $this->inner->onKernelRequest($event);
65
    }
66
67 5
    public function onKernelResponse(FilterResponseEvent $event)
68
    {
69 5
        if (!$event->isMasterRequest()) {
70
            return;
71
        }
72
73 5
        $varyHeaders = array_map('strtolower', $event->getResponse()->getVary());
74 5
        $relevantHeaders = array_merge($this->userIdentifierHeaders, [$this->userHashHeader]);
75
76
        // Call default behaviour if it's an irrelevant request for the user context
77 5
        if (0 === count(array_intersect($varyHeaders, $relevantHeaders))) {
78 2
            $this->inner->onKernelResponse($event);
79
        }
80
81
        // noop, see class description
82 5
    }
83
84
    public static function getSubscribedEvents(): array
85
    {
86
        return BaseSessionListener::getSubscribedEvents();
87
    }
88
}
89