Completed
Push — master ( 155c91...41d5fe )
by Axel
05:02
created

TwigRuntime::displayPathAsString()   B

Complexity

Conditions 11
Paths 22

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 11
eloc 36
c 1
b 1
f 0
nc 22
nop 2
dl 0
loc 54
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Routes.
5
 *
6
 * @copyright Zikula contributors (Zikula)
7
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
8
 * @author Zikula contributors <[email protected]>.
9
 *
10
 * @see https://ziku.la
11
 *
12
 * @version Generated by ModuleStudio 1.5.0 (https://modulestudio.de).
13
 */
14
15
declare(strict_types=1);
16
17
namespace Zikula\RoutesModule\Twig;
18
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Zikula\RoutesModule\Entity\RouteEntity;
21
use Zikula\RoutesModule\Helper\PathBuilderHelper;
22
use Zikula\RoutesModule\Twig\Base\AbstractTwigRuntime;
23
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
24
25
/**
26
 * Twig runtime implementation class.
27
 */
28
class TwigRuntime extends AbstractTwigRuntime
29
{
30
    /**
31
     * @var ContainerInterface
32
     */
33
    private $container;
34
35
    /**
36
     * @var LocaleApiInterface
37
     */
38
    private $localeApi;
39
40
    /**
41
     * @var PathBuilderHelper
42
     */
43
    private $pathBuilderHelper;
44
45
    /**
46
     * The zikularoutesmodule_arrayToString filter displays the content of a given array.
47
     * Example:
48
     *    {{ route.defaults|zikularoutesmodule_arrayToString }}
49
     */
50
    public function displayArrayAsString(array $input = []): string
51
    {
52
        return '<pre>' . print_r($input, true) . '</pre>';
53
    }
54
55
    /**
56
     * The zikularoutesmodule_pathToString filter displays a route's path.
57
     * Example:
58
     *    {{ route.path|zikularoutesmodule_pathToString(route) }}
59
     */
60
    public function displayPathAsString(string $path, RouteEntity $route): string
61
    {
62
        $prefix = '';
63
        $translationPrefix = $route->getTranslationPrefix();
64
        if (!empty($translationPrefix)) {
65
            $prefix = '/' . $translationPrefix;
66
        }
67
68
        if ($route->getTranslatable()) {
69
            $languages = $this->localeApi->getSupportedLocales();
70
            $isRequiredLangParameter = $this->variableApi->getSystemVar('languageurl', 0);
71
            if (!$isRequiredLangParameter) {
72
                $defaultLanguage = $this->variableApi->getSystemVar('locale');
73
                unset($languages[array_search($defaultLanguage, $languages, true)]);
74
            }
75
            if (count($languages) > 0) {
76
                $prefix = ($isRequiredLangParameter ? '/' : '{/') . implode('|', $languages) . ($isRequiredLangParameter ? '' : '}');
77
            }
78
        }
79
80
        $prefix = htmlspecialchars($prefix);
81
        $path = htmlspecialchars(
82
            $this->pathBuilderHelper->getPathWithBundlePrefix($route)
83
        );
84
85
        $container = $this->container;
86
        $path = preg_replace_callback('#%(.*?)%#', static function ($matches) use ($container) {
87
            return '<abbr title="' . htmlspecialchars($matches[0]) . '">'
88
                . htmlspecialchars($container->getParameter($matches[1]))
89
                . '</abbr>'
90
            ;
91
        }, $path);
92
93
        $defaults = $route->getDefaults();
94
        $requirements = $route->getRequirements();
95
        $path = preg_replace_callback('#{(.*?)}#', function ($matches) use ($defaults, $requirements) {
96
            $title = '';
97
            if (isset($defaults[$matches[1]])) {
98
                $title .= $this->trans('Default: %value%', ['%value%' => htmlspecialchars($defaults[$matches[1]])]);
99
            }
100
            if (isset($requirements[$matches[1]])) {
101
                if ('' !== $title) {
102
                    $title .= ' | ';
103
                }
104
                $title .= $this->trans('Requirement: %value%', ['%value%' => htmlspecialchars($requirements[$matches[1]])]);
105
            }
106
            if ('' === $title) {
107
                return $matches[0];
108
            }
109
110
            return '<abbr title="' . $title . '">' . $matches[0] . '</abbr>';
111
        }, $path);
112
113
        return $prefix . '<strong>' . $path . '</strong>';
114
    }
115
116
    /**
117
     * @required
118
     */
119
    public function setAdditionalDependencies(
120
        ContainerInterface $container,
121
        LocaleApiInterface $localeApi,
122
        PathBuilderHelper $pathBuilderHelper
123
    ): void {
124
        $this->container = $container;
125
        $this->localeApi = $localeApi;
126
        $this->pathBuilderHelper = $pathBuilderHelper;
127
    }
128
}
129