Md2HtmlExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 3
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFilters() 0 6 1
A getFunctions() 0 6 1
A markdownToHtml() 0 4 1
A getLocales() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\TemplateEngine\Twig\Extension;
16
17
use Acme\PhpExtension\String\Markdown;
18
use Symfony\Component\Intl\Intl;
19
use Twig\Extension\AbstractExtension;
20
use Twig\TwigFilter;
21
use Twig\TwigFunction;
22
23
/**
24
 * This Twig extension adds a new 'md2html' filter to easily transform Markdown
25
 * contents into HTML contents inside Twig templates.
26
 *
27
 * See https://symfony.com/doc/current/cookbook/templating/twig_extension.html
28
 *
29
 * @author Ryan Weaver <[email protected]>
30
 * @author Javier Eguiluz <[email protected]>
31
 * @author Julien ITARD <[email protected]>
32
 */
33
class Md2HtmlExtension extends AbstractExtension
34
{
35
    /**
36
     * @var Markdown
37
     */
38
    private $parser;
39
40
    /**
41
     * @var array
42
     */
43
    private $localeCodes;
44
45
    /**
46
     * @var array
47
     */
48
    private $locales;
49
50
    public function __construct(Markdown $parser, string $locales)
51
    {
52
        $this->parser = $parser;
53
        $this->localeCodes = explode('|', $locales);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getFilters(): array
60
    {
61
        return [
62
            new TwigFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
63
        ];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getFunctions(): array
70
    {
71
        return [
72
            new TwigFunction('locales', [$this, 'getLocales']),
73
        ];
74
    }
75
76
    /**
77
     * Transforms the given Markdown content into HTML content.
78
     */
79
    public function markdownToHtml(string $content): string
80
    {
81
        return $this->parser->toHtml($content);
82
    }
83
84
    /**
85
     * Takes the list of codes of the locales (languages) enabled in the
86
     * application and returns an array with the name of each locale written
87
     * in its own language (e.g. English, Français, Español, etc.).
88
     */
89
    public function getLocales(): array
90
    {
91
        if ($this->locales !== null) {
92
            return $this->locales;
93
        }
94
95
        $this->locales = [];
96
        foreach ($this->localeCodes as $localeCode) {
97
            $this->locales[] = ['code' => $localeCode, 'name' => Intl::getLocaleBundle()->getLocaleName($localeCode, $localeCode)];
98
        }
99
100
        return $this->locales;
101
    }
102
}
103