I18nRouter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 108
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setContext() 0 4 1
A getContext() 0 4 1
A getRouteCollection() 0 4 1
A generate() 0 15 3
A match() 0 4 1
A warmUp() 0 6 2
1
<?php
2
3
namespace FrankDeJonge\SymfonyI18nRouting\Routing;
4
5
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
6
use Symfony\Component\Routing\Exception\InvalidParameterException;
7
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
8
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
9
use Symfony\Component\Routing\Exception\NoConfigurationException;
10
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
11
use Symfony\Component\Routing\Exception\RouteNotFoundException;
12
use Symfony\Component\Routing\RequestContext;
13
use Symfony\Component\Routing\RouterInterface;
14
15
class I18nRouter implements RouterInterface, WarmableInterface
16
{
17
    /**
18
     * @var RouterInterface
19
     */
20
    private $router;
21
22
    /**
23
     * @var string
24
     */
25
    private $defaultLocale;
26
27 4
    public function __construct(RouterInterface $router, string $defaultLocale)
28
    {
29 4
        $this->router = $router;
30 4
        $this->defaultLocale = $defaultLocale;
31 4
    }
32
33 2
    public function setContext(RequestContext $context)
34
    {
35 2
        $this->router->setContext($context);
36 2
    }
37
38 2
    public function getContext()
39
    {
40 2
        return $this->router->getContext();
41
    }
42
43 2
    public function getRouteCollection()
44
    {
45 2
        return $this->router->getRouteCollection();
46
    }
47
48
    /**
49
     * Generates a URL or path for a specific route based on the given parameters.
50
     *
51
     * Parameters that reference placeholders in the route pattern will substitute them in the
52
     * path or host. Extra params are added as query string to the URL.
53
     *
54
     * When the passed reference type cannot be generated for the route because it requires a different
55
     * host or scheme than the current one, the method will return a more comprehensive reference
56
     * that includes the required params. For example, when you call this method with $referenceType = ABSOLUTE_PATH
57
     * but the route requires the https scheme whereas the current scheme is http, it will instead return an
58
     * ABSOLUTE_URL with the https scheme and the current host. This makes sure the generated URL matches
59
     * the route in any case.
60
     *
61
     * If there is no route with the given name, the generator must throw the RouteNotFoundException.
62
     *
63
     * The special parameter _fragment will be used as the document fragment suffixed to the final URL.
64
     *
65
     * @param string $name          The name of the route
66
     * @param mixed  $parameters    An array of parameters
67
     * @param int    $referenceType The type of reference to be generated (one of the constants)
68
     *
69
     * @return string The generated URL
70
     *
71
     * @throws RouteNotFoundException              If the named route doesn't exist
72
     * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
73
     * @throws InvalidParameterException           When a parameter value for a placeholder is not correct because
74
     *                                             it does not match the requirement
75
     */
76 2
    public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
77
    {
78 2
        $locale = $parameters['_locale']
79 2
            ?? $this->router->getContext()->getParameter('_locale')
80 2
                ?: $this->defaultLocale;
81 2
        $i18nParameters = $parameters;
82 2
        unset($i18nParameters['_locale']);
83 2
        $i18nRouteName = "{$name}.{$locale}";
84
85
        try {
86 2
            return $this->router->generate($i18nRouteName, $i18nParameters, $referenceType);
87 2
        } catch (RouteNotFoundException $exception) {
88 2
            return $this->router->generate($name, $parameters, $referenceType);
89
        }
90
    }
91
92
    /**
93
     * Tries to match a URL path with a set of routes.
94
     *
95
     * If the matcher can not find information, it must throw one of the exceptions documented
96
     * below.
97
     *
98
     * @param string $pathinfo The path info to be parsed (raw format, i.e. not urldecoded)
99
     *
100
     * @return array An array of parameters
101
     *
102
     * @throws NoConfigurationException  If no routing configuration could be found
103
     * @throws ResourceNotFoundException If the resource could not be found
104
     * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
105
     */
106 2
    public function match($pathinfo)
107
    {
108 2
        return $this->router->match($pathinfo);
109
    }
110
111
    /**
112
     * Warms up the cache.
113
     *
114
     * @param string $cacheDir The cache directory
115
     */
116 2
    public function warmUp($cacheDir)
117
    {
118 2
        if ($this->router instanceof WarmableInterface) {
119 2
            $this->router->warmUp($cacheDir);
120
        }
121
    }
122
}