CacheListener   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 7
dl 0
loc 84
rs 10
c 0
b 0
f 0
ccs 7
cts 42
cp 0.1666

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A attach() 0 6 1
A getCache() 0 25 4
A saveCache() 0 19 3
A setCacheDir() 0 7 1
A genCacheName() 0 6 1
1
<?php
2
3
namespace Columnis\Model;
4
5
use Zend\EventManager\AbstractListenerAggregate;
6
use Zend\EventManager\EventManagerInterface;
7
use Zend\Mvc\MvcEvent;
8
use Zend\Mvc\Router\RouteMatch;
9
10
class CacheListener extends AbstractListenerAggregate
11
{
12
13
    protected $listeners = array();
14
    protected $cacheService;
15
16 1
    public function __construct(HtmlCache $cacheService)
17
    {
18
        // We store the cache service generated by Zend\Cache from the service manager
19
        //$cacheService->setExtension('');
20 1
        $this->cacheService = $cacheService;
21 1
    }
22
23 1
    public function attach(EventManagerInterface $events)
24
    {
25
        // The AbstractListenerAggregate we are extending from allows us to attach our even listeners
26 1
        $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'getCache'), -1000);
27 1
        $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, array($this, 'saveCache'), -10000);
28 1
    }
29
30
    public function getCache(MvcEvent $event)
31
    {
32
        $match = $event->getRouteMatch();
33
34
        // is valid route?
35
        if (!$match) {
36
            return;
37
        }
38
39
        // does our route have the cache flag set to true?
40
        if ($match->getParam('cache')) {
41
            $cacheKey = $this->genCacheName($match);
42
43
            // get the cache page for this route
44
            $data = $this->cacheService->getItem($cacheKey);
45
46
            // ensure we have found something valid
47
            if ($data !== null) {
48
                $response = $event->getResponse();
49
                $response->setContent($data);
50
51
                return $response;
52
            }
53
        }
54
    }
55
56
    public function saveCache(MvcEvent $event)
57
    {
58
        $match = $event->getRouteMatch();
59
60
        // is valid route?
61
        if (!$match) {
62
            return;
63
        }
64
65
        // does our route have the cache flag set to true?
66
        if ($match->getParam('cache')) {
67
            $response = $event->getResponse();
68
            $data = $response->getContent();
69
70
            $this->setCacheDir($match);
71
            $cacheKey = $this->genCacheName($match);
72
            $this->cacheService->setItem($cacheKey, $data);
73
        }
74
    }
75
76
    public function setCacheDir(RouteMatch $match)
77
    {
78
        $params = $match->getParams();
79
        $lang = $params['lang'];
80
        $options = $this->cacheService->getOptions();
81
        $options->setNamespace($lang);
82
    }
83
 
84
    /**
85
     * @param \Zend\Mvc\Router\RouteMatch $match
86
     */
87
    public function genCacheName(RouteMatch $match)
88
    {
89
        $params = $match->getParams();
90
        $pageId = (int)$params['pageId'];
91
        return $pageId;
92
    }
93
}
94