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

AppExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
eloc 17
c 7
b 2
f 0
dl 0
loc 37
rs 10
ccs 0
cts 24
cp 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 1
A renderJavascriptLink() 0 12 4
A __construct() 0 3 1
A getFunctions() 0 6 1
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