Completed
Push — dev-master ( 6fb8f5...710732 )
by Karol
08:45
created

RouterDecorator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

9 Methods

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