Completed
Push — master ( 58748f...bc04d6 )
by Dev
04:22
created

AppExtension::renderMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use Cocur\Slugify\Slugify;
6
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
7
use PiedWeb\CMSBundle\Service\PageCanonicalService;
8
use PiedWeb\RenderAttributes\AttributesTrait;
9
use Twig\Extension\AbstractExtension;
10
use Twig\TwigFilter;
11
use Twig\TwigFunction;
12
use Twig_Environment;
13
14
class AppExtension extends AbstractExtension
15
{
16
    use AttributesTrait;
17
18
    public function __construct(PageCanonicalService $pageCanonical)
19
    {
20
        $this->pageCanonical = $pageCanonical;
21
    }
22
23
    public function getFilters()
24
    {
25
        return [
26
            //new TwigFilter('markdown', [AppExtension::class, 'markdownToHtml'], ['is_safe' => ['all']]),
27
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
28
            new TwigFilter(
29
                'punctuation_beautifer',
30
                [AppExtension::class, 'punctuationBeautifer'],
31
                ['is_safe' => ['html']]
32
            ),
33
        ];
34
    }
35
36
    public static function convertMarkdownImage(string $body)
37
    {
38
        return preg_replace(
39
            '/(?:!\[(.*?)\]\((.*?)\))/',
40
            '{%'
41
            .PHP_EOL.'    include "@PiedWebCMS/component/_inline_image.html.twig" with {'
42
            .PHP_EOL.'        "image_src" : "$2",'
43
            .PHP_EOL.'        "image_alt" : "$1"'
44
            .PHP_EOL.'    } only'
45
            .PHP_EOL.'%}'.PHP_EOL,
46
            $body
47
        );
48
    }
49
50
    public function getFunctions()
51
    {
52
        return [
53
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
54
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
55
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [
56
                'is_safe' => ['html'],
57
                'needs_environment' => true,
58
            ]),
59
            new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [
60
                'is_safe' => ['html'],
61
                'needs_environment' => true,
62
            ]),
63
            new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [
64
                'is_safe' => ['html'],
65
                'needs_environment' => true,
66
            ]),
67
            new TwigFunction(
68
                'bookmark', // = anchor
69
                [AppExtension::class, 'renderTxtAnchor'],
70
                ['is_safe' => ['html'], 'needs_environment' => true]
71
            ),
72
            new TwigFunction(
73
                'anchor', // = bookmark
74
                [AppExtension::class, 'renderTxtAnchor'],
75
                ['is_safe' => ['html'], 'needs_environment' => true]
76
            ),
77
            new TwigFunction(
78
                'mail', // = bookmark
79
                [AppExtension::class, 'renderMail'],
80
                ['is_safe' => ['html'], 'needs_environment' => true]
81
            ),
82
            new TwigFunction(
83
                'isCurrentPage', // = bookmark
84
                [$this, 'isCurrentPage'],
85
                ['is_safe' => ['html'], 'needs_environment' => false]
86
            ),
87
        ];
88
    }
89
90
    public function isCurrentPage(string $uri, ?Page $currentPage)
91
    {
92
        return
93
            null === $currentPage || $uri != $this->pageCanonical->generatePathForPage($currentPage->getRealSlug())
1 ignored issue
show
Bug introduced by
The method getRealSlug() does not exist on PiedWeb\CMSBundle\Entity\PageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to PiedWeb\CMSBundle\Entity\PageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            null === $currentPage || $uri != $this->pageCanonical->generatePathForPage($currentPage->/** @scrutinizer ignore-call */ getRealSlug())
Loading history...
94
            ? false
95
            : true;
96
    }
97
98
    public static function renderTxtAnchor(Twig_Environment $env, $name)
99
    {
100
        $slugify = new Slugify();
101
        $name = $slugify->slugify($name);
102
103
        return $env->render('@PiedWebCMS/component/_txt_anchor.html.twig', ['name' => $name]);
104
    }
105
106
    public static function renderMail(Twig_Environment $env, $mail, $class = '')
107
    {
108
        $mail = str_rot13($mail);
109
110
        return $env->render('@PiedWebCMS/component/_mail.html.twig', ['mail' => $mail, 'class' => $class]);
111
    }
112
113
    public static function renderEncodedMail(Twig_Environment $env, $mail)
114
    {
115
        return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]);
116
    }
117
118
    public static function punctuationBeautifer($text)
119
    {
120
        return str_replace(
121
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
122
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
123
            $text
124
        );
125
    }
126
127
    public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = [])
128
    {
129
        if (0 === strpos($path, 'http://')) {
130
            $path = '-'.substr($path, 7);
131
        } elseif (0 === strpos($path, 'https://')) {
132
            $path = '_'.substr($path, 8);
133
        } elseif (0 === strpos($path, 'mailto:')) {
134
            $path = '@'.substr($path, 7);
135
        }
136
137
        return $env->render(
138
            '@PiedWebCMS/component/_javascript_link.html.twig',
139
            [
140
            'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]),
141
            'anchor' => $anchor,
142
            ]
143
        );
144
    }
145
}
146