Completed
Pull Request — 1.0 (#14)
by Karol
05:54
created

RouterDecorator::processParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
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\Routing\RequestContext;
10
use Symfony\Component\Routing\Route;
11
use Symfony\Component\Routing\RouterInterface;
12
13
class RouterDecorator implements RouterInterface
14
{
15
    use DecoratorTrait;
16
17
    protected $parametersProcessorFactory;
18
19 1
    public function __construct(RouterInterface $router, EncodeParametersProcessorFactory $parametersProcessorFactory)
20
    {
21 1
        $this->object = $router;
22 1
        $this->parametersProcessorFactory = $parametersProcessorFactory;
23 1
    }
24
25 1
    public function getRouter(): RouterInterface
26
    {
27 1
        return $this->object;
28
    }
29
30 1
    public function generate(
31
        $name,
32
        $parameters = [],
33
        $referenceType = RouterInterface::ABSOLUTE_PATH
34
    ): string {
35 1
        $this->processParameters($this->getRoute($name, $parameters), $parameters);
36
37 1
        return $this->getRouter()->generate($name, $parameters, $referenceType);
38
    }
39
40 1
    private function getRoute(string $name, array $parameters): ?Route
41
    {
42 1
        $routeCollection = $this->getRouter()->getRouteCollection();
43 1
        $route = $routeCollection->get($name);
44
45 1
        if (null === $route) {
46 1
            $locale = $parameters['_locale'] ?? $this->getRouter()->getContext()->getParameter('_locale');
47 1
            $route = $routeCollection->get(sprintf('%s.%s', $name, $locale));
48
        }
49
50 1
        return $route;
51
    }
52
53 1
    private function processParameters(?Route $route, array &$parameters): void
54
    {
55 1
        if (null !== $route) {
56 1
            $parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route);
57 1
            if ($parametersProcessor->needToProcess()) {
58 1
                $parameters = $parametersProcessor->process($parameters);
59
            }
60
        }
61 1
    }
62
63
    /**
64
     * @codeCoverageIgnore
65
     */
66
    public function setContext(RequestContext $context)
67
    {
68
        $this->getRouter()->setContext($context);
69
    }
70
71
    /**
72
     * @codeCoverageIgnore
73
     */
74
    public function getContext()
75
    {
76
        return $this->getRouter()->getContext();
77
    }
78
79
    /**
80
     * @codeCoverageIgnore
81
     */
82
    public function getRouteCollection()
83
    {
84
        return $this->getRouter()->getRouteCollection();
85
    }
86
87
    /**
88
     * @codeCoverageIgnore
89
     */
90
    public function match($pathinfo)
91
    {
92
        return $this->getRouter()->match($pathinfo);
93
    }
94
}
95