Completed
Push — http_cache_mvc ( 64ff00...c340cf )
by
unknown
29:34
created

HttpCacheResponseSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Bundle\EzPublishCoreBundle\EventSubscriber;
6
7
use eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator\ResponseCacheConfigurator;
8
use eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseTagger\ResponseTagger;
9
use eZ\Publish\Core\MVC\Symfony\View\CachableView;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
12
use Symfony\Component\HttpKernel\KernelEvents;
13
14
/**
15
 * Configures the Response HTTP cache properties.
16
 */
17
class HttpCacheResponseSubscriber implements EventSubscriberInterface
18
{
19
    /**
20
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseTagger\ResponseTagger
21
     */
22
    private $dispatcherTagger;
23
24
    /**
25
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator\ResponseCacheConfigurator
26
     */
27
    private $responseConfigurator;
28
29
    public function __construct(ResponseCacheConfigurator $responseConfigurator, ResponseTagger $dispatcherTagger)
30
    {
31
        $this->responseConfigurator = $responseConfigurator;
32
        $this->dispatcherTagger = $dispatcherTagger;
33
    }
34
35
    public static function getSubscribedEvents()
36
    {
37
        return [KernelEvents::RESPONSE => 'configureCache'];
38
    }
39
40
    public function configureCache(FilterResponseEvent $event)
41
    {
42
        $view = $event->getRequest()->attributes->get('view');
43
        if (!$view instanceof CachableView || !$view->isCacheEnabled()) {
44
            return;
45
        }
46
47
        $response = $event->getResponse();
48
        $this->responseConfigurator->enableCache($response);
49
        $this->responseConfigurator->setSharedMaxAge($response);
50
        $this->dispatcherTagger->tag($this->responseConfigurator, $response, $view);
51
    }
52
}
53