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