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

onRestRequest()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.5432

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
ccs 13
cts 19
cp 0.6842
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 15
nc 5
nop 1
crap 8.5432
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