Completed
Push — master ( 530ce9...1b2ae5 )
by Paweł
23:42 queued 20:31
created

HttpCacheSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 5
c 2
b 1
f 1
lcom 1
cbo 3
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 8 1
A clearContainers() 0 15 3
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Bundle\TemplateEngineBundle\EventSubscriber;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use SWP\Component\Common\Event\HttpCacheEvent;
18
use SWP\Bundle\TemplateEngineBundle\Model\Container;
19
use FOS\HttpCache\Exception\ExceptionCollection;
20
21
class HttpCacheSubscriber implements EventSubscriberInterface
22
{
23
    protected $cacheManager;
24
25
    protected $logger;
26
27
    public function __construct($cacheManager, $logger)
28
    {
29
        $this->cacheManager = $cacheManager;
30
        $this->logger = $logger;
31
    }
32
33
    public static function getSubscribedEvents()
34
    {
35
        return [
36
           HttpCacheEvent::EVENT_NAME => [
37
               ['clearContainers', 0],
38
           ],
39
       ];
40
    }
41
42
    public function clearContainers(HttpCacheEvent $event)
43
    {
44
        $this->cacheManager->invalidateRoute('swp_api_templates_list_containers');
45
        if ($event->getSubject() instanceof Container) {
46
            $this->cacheManager->invalidateRoute('swp_api_templates_get_container', [
47
                'id' => $event->getSubject()->getId(),
48
            ]);
49
        }
50
51
        try {
52
            $this->cacheManager->flush();
53
        } catch (ExceptionCollection $e) {
54
            $this->logger->error($e->getMessage());
55
        }
56
    }
57
}
58