Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

HttpCacheSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
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
15
namespace SWP\Bundle\CoreBundle\EventSubscriber;
16
17
use FOS\HttpCache\Exception\ExceptionCollection;
18
use FOS\HttpCacheBundle\CacheManager;
19
use Psr\Log\LoggerInterface;
20
use SWP\Component\Common\Event\HttpCacheEvent;
21
use SWP\Component\TemplatesSystem\Gimme\Model\ContainerInterface;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
class HttpCacheSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * @var CacheManager
28
     */
29
    protected $cacheManager;
30
31
    /**
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
36 1
    public function __construct(CacheManager $cacheManager, LoggerInterface $logger)
37
    {
38 1
        $this->cacheManager = $cacheManager;
39 1
        $this->logger = $logger;
40 1
    }
41
42 109
    public static function getSubscribedEvents()
43
    {
44
        return [
45 109
            HttpCacheEvent::EVENT_NAME => [
46
                ['clearCache', 0],
47 109
            ],
48
        ];
49
    }
50
51
    public function clearCache(HttpCacheEvent $event)
52
    {
53
        switch (true) {
54
            case $event->getSubject() instanceof ContainerInterface:
55
                $this->cacheManager->invalidateRoute('swp_api_templates_list_containers');
56
                $this->cacheManager->invalidateRoute('swp_api_templates_get_container', [
57
                    'id' => $event->getSubject()->getId(),
58
                ]);
59
                break;
60
        }
61
62
        try {
63
            $this->cacheManager->flush();
64
        } catch (ExceptionCollection $e) {
65
            $this->logger->error($e->getMessage());
66
        }
67
    }
68
}
69