Passed
Push — 2.0 ( 715fc9...37ee49 )
by Karol
01:53 queued 11s
created

RouterDecorator::warmUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
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
        $name,
33
        $parameters = [],
34
        $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($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