Passed
Push — 3.0 ( 64c387...184a41 )
by Karol
03:16 queued 12s
created

RouterDecorator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 89
rs 10
c 2
b 0
f 0
ccs 22
cts 22
cp 1
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRouter() 0 3 1
A getContext() 0 3 1
A generate() 0 8 1
A getRouteCollection() 0 3 1
A getRoute() 0 11 2
A warmUp() 0 4 2
A setContext() 0 3 1
A match() 0 3 1
A processParameters() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pgs\HashIdBundle\Decorator;
6
7
use Pgs\HashIdBundle\ParametersProcessor\Factory\EncodeParametersProcessorFactory;
8
use Pgs\HashIdBundle\Traits\DecoratorTrait;
9
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
10
use Symfony\Component\Routing\RequestContext;
11
use Symfony\Component\Routing\Route;
12
use Symfony\Component\Routing\RouterInterface;
13
14
class RouterDecorator implements RouterInterface, WarmableInterface
15
{
16
    use DecoratorTrait;
17
18
    protected $parametersProcessorFactory;
19
20 1
    public function __construct(RouterInterface $router, EncodeParametersProcessorFactory $parametersProcessorFactory)
21
    {
22 1
        $this->object = $router;
23 1
        $this->parametersProcessorFactory = $parametersProcessorFactory;
24 1
    }
25
26 1
    public function getRouter(): RouterInterface
27
    {
28 1
        return $this->object;
29
    }
30
31 1
    public function generate(
32
        string $name,
33
        array $parameters = [],
34
        int $referenceType = RouterInterface::ABSOLUTE_PATH
35
    ): string {
36 1
        $this->processParameters($this->getRoute($name, $parameters), $parameters);
37
38 1
        return $this->getRouter()->generate($name, $parameters, $referenceType);
39
    }
40
41 1
    private function getRoute(string $name, array $parameters): ?Route
42
    {
43 1
        $routeCollection = $this->getRouter()->getRouteCollection();
44 1
        $route = $routeCollection->get($name);
45
46 1
        if (null === $route) {
47 1
            $locale = $parameters['_locale'] ?? $this->getRouter()->getContext()->getParameter('_locale');
48 1
            $route = $routeCollection->get(sprintf('%s.%s', $name, $locale));
49
        }
50
51 1
        return $route;
52
    }
53
54 1
    private function processParameters(?Route $route, array &$parameters): void
55
    {
56 1
        if (null !== $route) {
57 1
            $parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route);
58 1
            if ($parametersProcessor->needToProcess()) {
59 1
                $parameters = $parametersProcessor->process($parameters);
60
            }
61
        }
62 1
    }
63
64
    /**
65
     * @codeCoverageIgnore
66
     */
67
    public function setContext(RequestContext $context)
68
    {
69
        $this->getRouter()->setContext($context);
70
    }
71
72
    /**
73
     * @codeCoverageIgnore
74
     */
75
    public function getContext()
76
    {
77
        return $this->getRouter()->getContext();
78
    }
79
80
    /**
81
     * @codeCoverageIgnore
82
     */
83
    public function getRouteCollection()
84
    {
85
        return $this->getRouter()->getRouteCollection();
86
    }
87
88
    /**
89
     * @codeCoverageIgnore
90
     */
91
    public function match(string $pathinfo)
92
    {
93
        return $this->getRouter()->match($pathinfo);
94
    }
95
96
    /**
97
     * @codeCoverageIgnore
98
     */
99
    public function warmUp($cacheDir)
100
    {
101
        if ($this->getRouter() instanceof WarmableInterface) {
102
            $this->getRouter()->warmUp($cacheDir);
103
        }
104
    }
105
}
106