MarkdownHelpers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 6 1
A __invoke() 0 7 2
A setParsedown() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\View\Mustache;
6
7
// From Mustache
8
use Mustache_LambdaHelper as LambdaHelper;
9
10
// From 'erusev/parsedown'
11
use Parsedown;
12
13
/**
14
 * Mustache helpers for rendering Markdown syntax.
15
 */
16
class MarkdownHelpers implements HelpersInterface
17
{
18
    /**
19
     * Store the Markdown parser.
20
     *
21
     * @var Parsedown
22
     */
23
    private $parsedown;
24
25
    /**
26
     * @param array $data Class Dependencies.
27
     */
28
    public function __construct(array $data)
29
    {
30
        $this->setParsedown($data['parsedown']);
31
    }
32
33
    /**
34
     * Retrieve the helpers.
35
     *
36
     * @return array
37
     */
38
    public function toArray(): array
39
    {
40
        return [
41
            'markdown' => $this,
42
        ];
43
    }
44
45
    /**
46
     * Magic: Render the Mustache section.
47
     *
48
     * @param  string            $text   The Markdown text to parse.
49
     * @param  LambdaHelper|null $helper For rendering strings in the current context.
50
     * @return string
51
     */
52
    public function __invoke($text, LambdaHelper $helper = null): string
53
    {
54
        if ($helper !== null) {
55
            $text = $helper->render($text);
56
        }
57
        return $this->parsedown->text($text);
58
    }
59
60
    /**
61
     * Set the Markdown parser.
62
     *
63
     * @param  Parsedown $parser Thar Markdown parser.
64
     * @return void
65
     */
66
    private function setParsedown(Parsedown $parser): void
67
    {
68
        $this->parsedown = $parser;
69
    }
70
}
71