Completed
Push — master ( 39e189...a82ea3 )
by Dev
13:08
created

AppExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use PiedWeb\CMSBundle\Service\PageCanonicalService;
6
use PiedWeb\RenderAttributes\AttributesTrait;
7
use Twig\Extension\AbstractExtension;
8
use Twig\TwigFilter;
9
use Twig\TwigFunction;
10
11
class AppExtension extends AbstractExtension
12
{
13
    use AttributesTrait;
14
15
    public function __construct(PageCanonicalService $pageCanonical)
16
    {
17
        $this->pageCanonical = $pageCanonical;
18
    }
19
20
    public function getFilters()
21
    {
22
        return [
23
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
24
        ];
25
    }
26
27
    public function getFunctions()
28
    {
29
        return [
30
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
31
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
32
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], ['is_safe' => ['html']]),
33
        ];
34
    }
35
36
    public static function renderJavascriptLink($anchor, $path, $attr = [])
37
    {
38
        if (0 === strpos($path, 'http://')) {
39
            $path = '-'.substr($path, 7);
40
        } elseif (0 === strpos($path, 'https://')) {
41
            $path = '_'.substr($path, 8);
42
        } elseif (0 === strpos($path, 'mailto:')) {
43
            $path = '@'.substr($path, 7);
44
        }
45
46
        return '<span'.self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)])
47
            .' rel=nofollow>'.$anchor.'</span>';
48
    }
49
}
50