Markdown::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
c 3
b 0
f 0
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\Service;
8
9
use MaglMarkdown\Adapter\MarkdownAdapterInterface;
10
use Zend\EventManager\EventManagerInterface;
11
12
class Markdown
13
{
14
15
    /**
16
     *
17
     * @var MarkdownAdapterInterface
18
     */
19
    private $markdownAdapter;
20
21
    /**
22
     *
23
     * @var EventManagerInterface
24
     */
25
    private $eventManager;
26
27
    /**
28
     * Markdown constructor.
29
     *
30
     * @param MarkdownAdapterInterface   $markdownAdapter
31
     * @param EventManagerInterface|null $eventManager
32
     */
33 3
    public function __construct(MarkdownAdapterInterface $markdownAdapter, EventManagerInterface $eventManager = null)
34
    {
35 3
        $this->markdownAdapter = $markdownAdapter;
36 3
        $this->eventManager = $eventManager;
37 3
    }
38
39 3
    public function render($markdown)
40
    {
41
        // first check if there's something within the cache
42 3
        $cachedMarkdown = $this->triggerEvent('markdown.render.pre', array('markdown' => $markdown));
43 3
        if (false !== $cachedMarkdown) {
44 1
            return $cachedMarkdown;
45
        }
46
47
        // now render, it seems cache is not active
48
        // or nothing was found within the cache
49 2
        $renderedMarkdown = $this->markdownAdapter->transformText($markdown);
50
51
        // save the rendered markdown to the cache
52 2
        $this->triggerEvent('markdown.render.post', array(
53 2
            'markdown' => $markdown,
54 2
            'renderedMarkdown' => $renderedMarkdown,
55
        ));
56
57 2
        return $renderedMarkdown;
58
    }
59
60 3
    private function triggerEvent($event, $args)
61
    {
62
        // if there's no eventmanager, we don't need to trigger anything
63 3
        if (!$this->eventManager) {
64 1
            return false;
65
        }
66
67
        // triggering the event and return result, if event stopped
68 2
        $result = $this->eventManager->trigger($event, $this, $args);
69 2
        if ($result->stopped()) {
70 1
            return $result->last();
71
        }
72
73 1
        return false;
74
    }
75
}
76