RouterDecorator::getContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        $route = $this->getRouter()->getRouteCollection()->get($name);
37 1
        $this->processParameters($route, $parameters);
38
39 1
        return $this->getRouter()->generate($name, $parameters, $referenceType);
40
    }
41
42 1
    private function processParameters(?Route $route, array &$parameters): void
43
    {
44 1
        if (null !== $route) {
45 1
            $parametersProcessor = $this->parametersProcessorFactory->createRouteEncodeParametersProcessor($route);
46 1
            if ($parametersProcessor->needToProcess()) {
47 1
                $parameters = $parametersProcessor->process($parameters);
48
            }
49
        }
50 1
    }
51
52
    /**
53
     * @codeCoverageIgnore
54
     */
55
    public function setContext(RequestContext $context)
56
    {
57
        $this->getRouter()->setContext($context);
58
    }
59
60
    /**
61
     * @codeCoverageIgnore
62
     */
63
    public function getContext()
64
    {
65
        return $this->getRouter()->getContext();
66
    }
67
68
    /**
69
     * @codeCoverageIgnore
70
     */
71
    public function getRouteCollection()
72
    {
73
        return $this->getRouter()->getRouteCollection();
74
    }
75
76
    /**
77
     * @codeCoverageIgnore
78
     */
79
    public function match(string $pathinfo)
80
    {
81
        return $this->getRouter()->match($pathinfo);
82
    }
83
84
    /**
85
     * @codeCoverageIgnore
86
     */
87
    public function warmUp($cacheDir)
88
    {
89
        if ($this->getRouter() instanceof WarmableInterface) {
90
            $this->getRouter()->warmUp($cacheDir);
91
        }
92
    }
93
}
94