Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

SchemaCacheInvalidationResponseListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 73.91%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 60
ccs 17
cts 23
cp 0.7391
rs 10
c 2
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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        $cacheInvalidationMapKey cache key of invalidation map
35
     */
36 4
    public function __construct(CacheProvider $cache, $cacheInvalidationMapKey)
37
    {
38 4
        $this->cache = $cache;
39 4
        $this->cacheInvalidationMapKey = $cacheInvalidationMapKey;
40 4
    }
41
42
    /**
43
     * Invalidates stuff if where are in the invalidation map
44
     *
45
     * @param RestEvent $event rest event
46
     *
47
     * @return void
48
     */
49 4
    public function onRestRequest(RestEvent $event)
50
    {
51 4
        if ($event->getRequest()->getMethod() == 'GET') {
52 4
            return;
53
        }
54
55 4
        $controller = $event->getController();
56
57 4
        if ($controller instanceof RestController &&
58 4
            $controller->getModel() instanceof DocumentModel &&
59 4
            $this->cache->contains($this->cacheInvalidationMapKey)
60 2
        ) {
61 2
            $className = $controller->getModel()->getEntityClass();
62 2
            $invalidationMap = $this->cache->fetch($this->cacheInvalidationMapKey);
63
64 2
            if (!isset($invalidationMap[$className])) {
65 2
                return;
66
            }
67
68
            foreach ($invalidationMap[$className] as $cacheKey) {
69
                $this->cache->delete($cacheKey);
70
            }
71
72
            unset($invalidationMap[$className]);
73
74
            $this->cache->save($this->cacheInvalidationMapKey, $invalidationMap);
75
        }
76 2
    }
77
}
78