CacheListener::preRender()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * @author Matthias Glaub <[email protected]>
5
 */
6
7
namespace MaglMarkdown\Cache;
8
9
use Zend\Cache\Storage\StorageInterface;
10
use Zend\EventManager\Event;
11
use Zend\EventManager\EventManagerInterface;
12
use Zend\EventManager\ListenerAggregateInterface;
13
14
/**
15
 * Class CacheListener
16
 *
17
 * @package MaglMarkdown\Cache
18
 */
19
class CacheListener implements ListenerAggregateInterface
20
{
21
    /**
22
     *
23
     * @var StorageInterface
24
     */
25
    private $cache;
26
27
    /**
28
     * @var callable[]
29
     */
30
    protected $listeners = array();
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 1
    public function detach(EventManagerInterface $events)
36
    {
37 1
        foreach ($this->listeners as $index => $callback) {
38 1
            $events->detach($callback);
39 1
            unset($this->listeners[$index]);
40
        }
41 1
    }
42
43 2
    public function __construct(StorageInterface $cache)
44
    {
45 2
        $this->cache = $cache;
46 2
    }
47
48
    /**
49
     * @param EventManagerInterface $events
50
     * @param int                   $priority
51
     */
52 1
    public function attach(EventManagerInterface $events, $priority = 1)
53
    {
54 1
        $this->listeners[] = $events->attach('markdown.render.pre', array($this, 'preRender'), $priority);
55 1
        $this->listeners[] = $events->attach('markdown.render.post', array($this, 'postRender'), $priority);
56 1
    }
57
58
    /**
59
     * Searches the cache for rendered markdown.
60
     *
61
     * @param  \Zend\EventManager\Event $event
62
     * @return mixed the rendered markdown, if found, false otherwise
63
     * @throws \Zend\Cache\Exception\ExceptionInterface
64
     */
65 1
    public function preRender(Event $event)
66
    {
67 1
        $markdown = $event->getParam('markdown');
68
69 1
        $markdownHash = md5($markdown);
70
71 1
        $success = false;
72 1
        $renderedMarkdown = $this->cache->getItem($markdownHash, $success);
73 1
        if ($success) {
74 1
            $event->stopPropagation(true);
75
76 1
            return $renderedMarkdown;
77
        }
78
79 1
        return false;
80
    }
81
82
    /**
83
     * Saves the rendered markdown within the cache.
84
     *
85
     * @param \Zend\EventManager\Event $event
86
     * @return mixed
87
     * @throws \Zend\Cache\Exception\ExceptionInterface
88
     */
89 1
    public function postRender(Event $event)
90
    {
91 1
        $markdown = $event->getParam('markdown');
92 1
        $renderedMarkdown = $event->getParam('renderedMarkdown');
93
94 1
        $markdownHash = md5($markdown);
95
96 1
        return $this->cache->setItem($markdownHash, $renderedMarkdown);
97
    }
98
}
99