Completed
Push — master ( 694fd3...8ef274 )
by Dev
10:42
created

AppExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 2 Features 0
Metric Value
eloc 39
c 8
b 2
f 0
dl 0
loc 77
ccs 0
cts 30
cp 0
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 5 1
A renderJavascriptLink() 0 13 4
A __construct() 0 3 1
A getFunctions() 0 21 1
A punctuationBeautifer() 0 6 1
A renderTxtBookbmark() 0 6 1
A renderEncodedMail() 0 3 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use Cocur\Slugify\Slugify;
6
use PiedWeb\CMSBundle\Service\PageCanonicalService;
7
use PiedWeb\RenderAttributes\AttributesTrait;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFilter;
10
use Twig\TwigFunction;
11
use Twig_Environment;
12
13
class AppExtension extends AbstractExtension
14
{
15
    use AttributesTrait;
16
17
    public function __construct(PageCanonicalService $pageCanonical)
18
    {
19
        $this->pageCanonical = $pageCanonical;
20
    }
21
22
    public function getFilters()
23
    {
24
        return [
25
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
26
            new TwigFilter('punctuation_beautifer', [AppExtension::class, 'punctuationBeautifer'], ['is_safe' => ['html']]),
27
        ];
28
    }
29
30
    public function getFunctions()
31
    {
32
        return [
33
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
34
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
35
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [
36
                'is_safe' => ['html'],
37
                'needs_environment' => true,
38
            ]),
39
            new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [
40
                'is_safe' => ['html'],
41
                'needs_environment' => true,
42
            ]),
43
            new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [
44
                'is_safe' => ['html'],
45
                'needs_environment' => true,
46
            ]),
47
            new TwigFunction(
48
                'bookmark',
49
                [AppExtension::class, 'renderTxtBookbmark'],
50
                ['is_safe' => ['html'], 'needs_environment' => true]
51
            ),
52
        ];
53
    }
54
55
    public static function renderTxtBookbmark(Twig_Environment $env, $name)
56
    {
57
        $slugify = new Slugify();
58
        $name = $slugify->slugify($name);
59
60
        return $env->render('@PiedWebCMS/component/_txt_bookmark.html.twig', ['name' => $name]);
61
    }
62
63
    public static function renderEncodedMail(Twig_Environment $env, $mail)
64
    {
65
        return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]);
66
    }
67
68
    public static function punctuationBeautifer($text)
69
    {
70
        return str_replace(
71
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
72
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
73
            $text
74
        );
75
    }
76
77
    public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = [])
78
    {
79
        if (0 === strpos($path, 'http://')) {
80
            $path = '-'.substr($path, 7);
81
        } elseif (0 === strpos($path, 'https://')) {
82
            $path = '_'.substr($path, 8);
83
        } elseif (0 === strpos($path, 'mailto:')) {
84
            $path = '@'.substr($path, 7);
85
        }
86
87
        return $env->render('@PiedWebCMS/component/_javascript_link.html.twig', [
88
            'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]),
89
            'anchor' => $anchor,
90
        ]);
91
    }
92
}
93