CurrentPathTranslationExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the I18n Routing Bundle.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\I18nRoutingBundle\Twig;
13
14
use BenatEspina\I18nRoutingBundle\Resolver\NotFoundLocaleResolver;
15
use BenatEspina\I18nRoutingBundle\Resolver\ParametersResolverDoesNotExist;
16
use BenatEspina\I18nRoutingBundle\Resolver\ParametersResolverRegistry;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19
20
/**
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class CurrentPathTranslationExtension extends \Twig_Extension
24
{
25
    private $requestStack;
26
    private $urlGenerator;
27
    private $parametersResolverRegistry;
28
    private $notFoundLocaleResolver;
29
30
    public function __construct(
31
        RequestStack $requestStack,
32
        UrlGeneratorInterface $urlGenerator,
33
        ParametersResolverRegistry $parametersResolverRegistry,
34
        NotFoundLocaleResolver $notFoundLocaleResolver
35
    ) {
36
        $this->requestStack = $requestStack;
37
        $this->urlGenerator = $urlGenerator;
38
        $this->parametersResolverRegistry = $parametersResolverRegistry;
39
        $this->notFoundLocaleResolver = $notFoundLocaleResolver;
40
    }
41
42
    public function getFunctions()
43
    {
44
        return [
45
            new \Twig_SimpleFunction('current_path_translation', [$this, 'currentPathTranslation']),
46
        ];
47
    }
48
49
    public function currentPathTranslation($newLocale, $parametersResolverAlias = null, $relative = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        $request = $this->requestStack->getMasterRequest();
52
53
        $locale = $request->getLocale();
54
        $name = $request->get('_route');
55
        $parameters = $request->get('_route_params');
56
        $parameters['_locale'] = $newLocale;
57
58
        try {
59
            $this->parametersResolver($parametersResolverAlias)->resolve($locale, $newLocale, $parameters);
60
61
            $url = $this->urlGenerator->generate(
62
                $name,
63
                $parameters,
64
                $relative
65
                    ? UrlGeneratorInterface::RELATIVE_PATH
66
                    : UrlGeneratorInterface::ABSOLUTE_PATH
67
            );
68
        } catch (\Exception $exception) {
69
            $url = $this->notFoundLocaleResolver->generateUrl($request, $locale, $newLocale);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getMasterRequest() on line 51 can be null; however, BenatEspina\I18nRoutingB...Resolver::generateUrl() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70
        }
71
72
        return $url;
73
    }
74
75
    private function parametersResolver($alias = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
76
    {
77
        if (null === $alias) {
78
            return $this->parametersResolverRegistry->getDefault();
79
        }
80
        if (!$this->parametersResolverRegistry->has($alias)) {
81
            throw new ParametersResolverDoesNotExist($alias);
82
        }
83
84
        return $this->parametersResolverRegistry->get($alias);
85
    }
86
}
87