Completed
Push — master ( 9c4c8b...f11e65 )
by Leny
30:19 queued 19:59
created

RoutingExtention::getPath()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 5.3846
cc 8
eloc 16
nc 9
nop 3
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Twig\Extension;
4
5
use Symfony\Bridge\Twig\Extension\RoutingExtension as BaseRoutingExtension;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Victoire\Bundle\I18nBundle\Resolver\LocaleResolver;
9
use Victoire\Bundle\PageBundle\Helper\PageHelper;
10
11
/**
12
 * class RoutingExtension.
13
 */
14
class RoutingExtention extends BaseRoutingExtension
15
{
16
    private $pageHelper;
17
    private $generator;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
    private $localeResolver;
19
20
    /**
21
     * @param PageHelper            $pageHelper
22
     * @param UrlGeneratorInterface $generator
23
     * @param LocaleResolver        $localeResolver
24
     */
25
    public function __construct(PageHelper $pageHelper, UrlGeneratorInterface $generator, LocaleResolver $localeResolver, RequestStack $requestStack)
26
    {
27
        $this->pageHelper = $pageHelper;
28
        $this->generator = $generator;
29
        $this->localeResolver = $localeResolver;
30
        $this->request = $requestStack->getCurrentRequest();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        parent::__construct($generator);
32
    }
33
34
    public function getPath($name, $parameters = [], $relative = false)
35
    {
36
        if ($name == 'victoire_core_page_show_by_id') {
37
            $params = ['viewId' => $parameters['viewId']];
38
            unset($parameters['viewId']);
39
            if (!empty($parameters['entityId'])) {
40
                $params['entityId'] = $parameters['entityId'];
41
                unset($parameters['entityId']);
42
            }
43
            $page = $this->pageHelper->findPageByParameters($params);
44
            $parameters['url'] = $page->getReference()->getUrl();
45
46
            $name = 'victoire_core_page_show';
47
        }
48
49
        $prefix = '';
50
        //if locale is passed (and different) and i18n strategy is "domain"
51
        if (!empty($parameters['_locale']) && $parameters['_locale'] != $this->request->getLocale() && $this->localeResolver->localePattern === LocaleResolver::PATTERN_DOMAIN) {
52
            $prefix = $this->getPrefix($parameters['_locale']);
53
            //if we set a prefix, we don't want an absolute path
54
            if ($prefix) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $prefix of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
                $relative = true;
56
            }
57
        }
58
59
        return $prefix.$this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
60
    }
61
62
    /**
63
     * If victoire_i18n.locale_pattern == domain, then we force the url rewrite with a valid host.
64
     *
65
     * @param $locale
66
     *
67
     * @return null
68
     */
69
    protected function getPrefix($locale)
70
    {
71
        foreach ($this->localeResolver->getDomainConfig() as $_domain => $_locale) {
72
            if ($_locale === $locale) {
73
                $urlPrefix = sprintf('%s://%s', $this->request->getScheme(), $_domain);
74
                if ($this->request->getPort()) {
75
                    $urlPrefix .= ':'.$this->request->getPort();
76
                }
77
78
                return $urlPrefix;
79
            }
80
        }
81
82
        return;
83
    }
84
}
85