RouterDecorator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 77
ccs 16
cts 16
cp 1
rs 10
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getContext() 0 3 1
A generate() 0 9 1
A getRouteCollection() 0 3 1
A warmUp() 0 4 2
A __construct() 0 4 1
A setContext() 0 3 1
A getRouter() 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
        $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