Completed
Push — ezp_31499_ez_path_ez_url ( 80f1d5 )
by
unknown
12:51
created

RoutingExtension::getUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
10
11
use eZ\Publish\API\Repository\Values\Content\Content;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
15
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface;
16
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference;
17
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
18
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19
use Twig\Extension\AbstractExtension;
20
use Twig\Node\Expression\ArrayExpression;
21
use Twig\Node\Expression\ConstantExpression;
22
use Twig\Node\Node;
23
use Twig\TwigFunction;
24
25
class RoutingExtension extends AbstractExtension
26
{
27
    /** @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\RouteReferenceGeneratorInterface */
28
    private $routeReferenceGenerator;
29
30
    /** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
31
    private $urlGenerator;
32
33
    public function __construct(
34
        RouteReferenceGeneratorInterface $routeReferenceGenerator,
35
        UrlGeneratorInterface $urlGenerator
36
    ) {
37
        $this->routeReferenceGenerator = $routeReferenceGenerator;
38
        $this->urlGenerator = $urlGenerator;
39
    }
40
41
    public function getFunctions(): array
42
    {
43
        return [
44
            new TwigFunction(
45
                'ez_route',
46
                [$this, 'getRouteReference']
47
            ),
48
            new TwigFunction(
49
                'ez_path',
50
                [$this, 'getPath'],
51
                ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]
52
            ),
53
            new TwigFunction(
54
                'ez_url',
55
                [$this, 'getUrl'],
56
                ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]
57
            ),
58
        ];
59
    }
60
61
    public function getName(): string
62
    {
63
        return 'ezpublish.routing';
64
    }
65
66
    /**
67
     * @param mixed $resource
68
     * @param array $params
69
     *
70
     * @return \eZ\Publish\Core\MVC\Symfony\Routing\RouteReference
71
     */
72
    public function getRouteReference($resource = null, $params = []): RouteReference
73
    {
74
        return $this->routeReferenceGenerator->generate($resource, $params);
75
    }
76
77 View Code Duplication
    public function getPath($name, array $parameters = [], bool $relative = false): string
78
    {
79
        $referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH;
80
        if (is_object($name)) {
81
            return $this->generateUrlForObject($name, $parameters, $referenceType);
82
        }
83
84
        return $this->urlGenerator->generate($name, $parameters, $referenceType);
85
    }
86
87 View Code Duplication
    public function getUrl($name, array $parameters = [], bool $schemeRelative = false): string
88
    {
89
        $referenceType = $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL;
90
        if (is_object($name)) {
91
            return $this->generateUrlForObject($name, $parameters, $referenceType);
92
        }
93
94
        return $this->urlGenerator->generate($name, $parameters, $referenceType);
95
    }
96
97
    /**
98
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
99
     */
100
    private function generateUrlForObject(object $object, array $parameters, int $referenceType): string
101
    {
102
        $routeName = null;
0 ignored issues
show
Unused Code introduced by
$routeName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
104
        if ($object instanceof Location) {
105
            $routeName = UrlAliasRouter::URL_ALIAS_ROUTE_NAME;
106
            $parameters += [
107
                'locationId' => $object->id,
108
            ];
109
        } elseif ($object instanceof Content || $object instanceof ContentInfo) {
110
            $routeName = UrlAliasRouter::URL_ALIAS_ROUTE_NAME;
111
            $parameters += [
112
                'contentId' => $object->id,
113
            ];
114
        } elseif ($object instanceof RouteReference) {
115
            $routeName = $object->getRoute();
116
            $parameters += $object->getParams();
117
        } else {
118
            throw new InvalidArgumentException('$object', 'Unsupported class: ' . get_class($object));
119
        }
120
121
        return $this->urlGenerator->generate($routeName, $parameters, $referenceType);
122
    }
123
124
    /**
125
     * Determines at compile time whether the generated URL will be safe and thus
126
     * saving the unneeded automatic escaping for performance reasons.
127
     *
128
     * @see \Symfony\Bridge\Twig\Extension\RoutingExtension::isUrlGenerationSafe
129
     */
130
    public function isUrlGenerationSafe(Node $argsNode): array
131
    {
132
        // support named arguments
133
        $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
134
            $argsNode->hasNode('1') ? $argsNode->getNode('1') : null
135
        );
136
137
        if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 &&
138
            (!$paramsNode->hasNode('1') || $paramsNode->getNode('1') instanceof ConstantExpression)
139
        ) {
140
            return ['html'];
141
        }
142
143
        return [];
144
    }
145
}
146