Test Setup Failed
Push — master ( d1bf52...dc45f2 )
by Mehmet
02:56
created

TwigExtensions::extendForGetUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Selami\View\Twig;
5
6
use Selami\Stdlib\CaseConverter;
7
use Selami\View\ExtensionsAbstract;
8
use Twig_Environment;
9
use InvalidArgumentException;
10
use BadMethodCallException;
11
use Twig_SimpleFunction;
12
13
/**
14
 * Class TwigExtensions extends ViewExtensionsAbstracts
15
 *
16
 * @package Selami\View\Twig
17
 */
18
class TwigExtensions extends ExtensionsAbstract
19
{
20
    private $twig;
21
    private $config;
22
23
    public function __construct(Twig_Environment $twig, array $config)
24
    {
25
        $this->twig = $twig;
26
        $this->config = $config;
27
        $this->loadFunctions();
28
    }
29
30 View Code Duplication
    protected function extendForGetUrl() : void
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $filter = new Twig_SimpleFunction(
33
            'getUrl',
34
            function (
35
                $alias,
36
                $params = []
37
            ) {
38
                $function = new Functions\GetUrl($this->config['base_url'], $this->config['aliases'], $alias, $params);
39
                return $function->run();
40
            },
41
            array('is_safe' => array('html'))
42
        );
43
        $this->twig->addFunction($filter);
44
    }
45
46 View Code Duplication
    protected function extendForWidget() : void
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $filter = new \Twig_SimpleFunction(
49
            'Widget_*_*',
50
            function ($widgetNameStr, $widgetActionStr, $args = []) {
51
52
53
                $function = new Functions\Widget($this->twig, $this->config, $widgetNameStr, $widgetActionStr, $args);
1 ignored issue
show
Compatibility introduced by
$this->twig of type object<Twig_Environment> is not a sub-type of object<Twig\Environment>. It seems like you assume a child class of the class Twig_Environment to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
54
                return $function->run();
55
56
            },
57
            array('is_safe' => array('html'))
58
        );
59
        $this->twig->addFunction($filter);
60
    }
61
62 View Code Duplication
    protected function extendForQueryParams() : void
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $filter = new \Twig_SimpleFunction(
65
            'queryParams',
66
            function ($queryParams, $prefix = '?') {
67
                return $prefix . http_build_query($queryParams);
68
            },
69
            array('is_safe' => array('html'))
70
        );
71
        $this->twig->addFunction($filter);
72
    }
73
74 View Code Duplication
    protected function extendForSiteUrl() : void
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $filter = new \Twig_SimpleFunction(
77
            'siteUrl',
78
            function ($path = '') {
79
                return $this->config['base_url'] . $path;
80
            },
81
            array('is_safe' => array('html'))
82
        );
83
        $this->twig->addFunction($filter);
84
    }
85
86
    protected function extendForVarDump() : void
87
    {
88
        $filter = new \Twig_SimpleFunction(
89
            'varDump',
90
            function ($args) {
91
                /**
92
            * @noinspection ForgottenDebugOutputInspection 
93
            */
94
                var_dump($args);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($args); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
95
            },
96
            array('is_safe' => array('html'))
97
        );
98
        $this->twig->addFunction($filter);
99
    }
100
101
    protected function extendForPagination() : void
102
    {
103
        /**
104
 * @noinspection MoreThanThreeArgumentsInspection 
105
*/
106
        $filter = new \Twig_SimpleFunction(
107
            'Pagination',
108
            function (
109
                int $total,
110
                int $current,
111
                string $linkTemplate,
112
                string $parentTemplate = '<ul class="pagination">(items)</ul>',
113
                string $itemTemplate = '<li class="(item_class)">(link)</li>',
114
                string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>',
115
                string $ellipsesTemplate = '<li><a>...</a></li>'
116
            ) {
117
                $items =  '';
118
                $pageGroupLimit = 3;
119
                $useEllipses = ($total >10) ? 1 : 0;
120
                $renderedEllipses = 0;
121
                $values = [
122
                    '(item_class)'  => '',
123
                    '(href)'        => '',
124
                    '(link_class)'  => '',
125
                    '(text)'  => ''
126
                ];
127
                for ($i=1; $i <= $total; $i++) {
128
                    $values['(link_class)'] = '';
129
                    $values['(item_class)'] = '';
130
                    if ($i === $current) {
131
                        $values['(link_class)'] = 'active';
132
                        $values['(item_class)'] = 'active';
133
                    }
134
                    $values['(text)'] = $i;
135
                    $values['(href)'] = str_replace('(page_num)', $i, $linkTemplate);
136
                    $link = strtr($linkItemTemplate, $values);
137
                    $useLink = 1;
138
                    if ($useEllipses === 1) {
139
                        $useLink = 0;
140
                        if (($i <= $pageGroupLimit)
141
                            ||  ($i >= ($current-1) && $i <= ($current+1))
142
                            || ($i >= ($total-2))
143
                        ) {
144
                            $useLink = 1;
145
                            $renderedEllipses = 0;
146
                        } else {
147
                            if ($renderedEllipses === 0) {
148
                                $items .= $ellipsesTemplate;
149
                            }
150
                            $renderedEllipses = 1;
151
                        }
152
                    }
153
                    if ($useLink === 1) {
154
                        $thisItemTemplate = str_replace(
155
                            array('(link)'),
156
                            array($link),
157
                            $itemTemplate
158
                        );
159
                        $items .= strtr($thisItemTemplate, $values);
160
                    }
161
                }
162
                return str_replace('(items)', $items, $parentTemplate);
163
            },
164
            array('is_safe' => array('html'))
165
        );
166
        $this->twig->addFunction($filter);
167
    }
168
}
169