Completed
Push — master ( 9ad7fa...c806a5 )
by Łukasz
55:51 queued 43:47
created

LocaleStrippingRouter::getRouteCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sylius\Bundle\ShopBundle\Router;
4
5
use Sylius\Component\Locale\Context\LocaleContextInterface;
6
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Symfony\Component\Routing\RequestContext;
9
use Symfony\Component\Routing\RouteCollection;
10
use Symfony\Component\Routing\RouterInterface;
11
12
final class LocaleStrippingRouter implements RouterInterface, WarmableInterface
13
{
14
    /**
15
     * @var RouterInterface
16
     */
17
    private $router;
18
19
    /**
20
     * @var LocaleContextInterface
21
     */
22
    private $localeContext;
23
24
    /**
25
     * @param RouterInterface $router
26
     * @param LocaleContextInterface $localeContext
27
     */
28
    public function __construct(RouterInterface $router, LocaleContextInterface $localeContext)
29
    {
30
        $this->router = $router;
31
        $this->localeContext = $localeContext;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function match($pathinfo): array
38
    {
39
        return $this->router->match($pathinfo);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string
46
    {
47
        $url = $this->router->generate($name, $parameters, $absolute);
48
49
        if (false === strpos($url, '_locale')) {
50
            return $url;
51
        }
52
53
        return $this->removeUnusedQueryArgument($url, '_locale', $this->localeContext->getLocaleCode());
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function setContext(RequestContext $context): void
60
    {
61
        $this->router->setContext($context);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getContext(): RequestContext
68
    {
69
        return $this->router->getContext();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRouteCollection(): RouteCollection
76
    {
77
        return $this->router->getRouteCollection();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function warmUp($cacheDir): void
84
    {
85
        if ($this->router instanceof WarmableInterface) {
86
            $this->router->warmUp($cacheDir);
87
        }
88
    }
89
90
    /**
91
     * @param string $url
92
     * @param string $key
93
     * @param string $value
94
     *
95
     * @return string
96
     */
97
    private function removeUnusedQueryArgument(string $url, string $key, string $value): string
98
    {
99
        $replace = [
100
            sprintf('&%s=%s', $key, $value) => '',
101
            sprintf('?%s=%s&', $key, $value) => '?',
102
            sprintf('?%s=%s', $key, $value) => '',
103
        ];
104
105
        return str_replace(array_keys($replace), array_values($replace), $url);
106
    }
107
}
108