Completed
Push — feature/other-validation ( f56062...7f372e )
by Narcotic
65:46
created

SchemaCacheInvalidationResponseListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C onRestRequest() 0 28 7
1
<?php
2
/**
3
 * Listener that invalidates (= removes) matching stuff from the Schema cache when appropriate
4
 */
5
6
namespace Graviton\SchemaBundle\Listener;
7
8
use Doctrine\Common\Cache\CacheProvider;
9
use Graviton\RestBundle\Controller\RestController;
10
use Graviton\RestBundle\Event\RestEvent;
11
use Graviton\RestBundle\Model\DocumentModel;
12
13
/**
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SchemaCacheInvalidationResponseListener
19
{
20
    /**
21
     * @var CacheProvider
22
     */
23
    private $cache;
24
25
    /**
26
     * @var string
27
     */
28
    private $cacheInvalidationMapKey;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param CacheProvider $cache                   cache
34
     * @param string        $cacheNamespace          cache namespace
35
     * @param string        $cacheInvalidationMapKey cache key of invalidation map
36
     */
37
    public function __construct(CacheProvider $cache, $cacheNamespace, $cacheInvalidationMapKey)
38
    {
39
        $cache->setNamespace($cacheNamespace);
40
        $this->cache = $cache;
41
        $this->cacheInvalidationMapKey = $cacheInvalidationMapKey;
42
    }
43
44
    /**
45
     * Invalidates stuff if where are in the invalidation map
46
     *
47
     * @param RestEvent $event rest event
48
     *
49
     * @return void
50
     */
51
    public function onRestRequest(RestEvent $event)
52
    {
53
        if ($event->getRequest()->getMethod() == 'GET') {
54
            return;
55
        }
56
57
        $controller = $event->getController();
58
59
        if ($controller instanceof RestController &&
60
            $controller->getModel() instanceof DocumentModel &&
61
            $this->cache->contains($this->cacheInvalidationMapKey)
62
        ) {
63
            $className = $controller->getModel()->getEntityClass();
64
            $invalidationMap = $this->cache->fetch($this->cacheInvalidationMapKey);
65
66
            if (!isset($invalidationMap[$className])) {
67
                return;
68
            }
69
70
            foreach ($invalidationMap[$className] as $cacheKey) {
71
                $this->cache->delete($cacheKey);
72
            }
73
74
            unset($invalidationMap[$className]);
75
76
            $this->cache->save($this->cacheInvalidationMapKey, $invalidationMap);
77
        }
78
    }
79
}
80