RouteRepository::getUrlGenerator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Bundle\JsonApiBundle\Routing;
5
6
use Symfony\Component\Routing\
7
{
8
    Generator\UrlGeneratorInterface,
9
    Generator\UrlGenerator,
10
    RouteCollection,
11
    RequestContext,
12
    Route
13
};
14
15
/**
16
 * Repository of routes
17
 *
18
 * @package Springfield\Component\HttpClient
19
 */
20
class RouteRepository
21
{
22
    /**
23
     * Prepared URL generator
24
     *
25
     * @var UrlGeneratorInterface
26
     */
27
    private $urlGenerator;
28
29
    /**
30
     * Route definitions
31
     *
32
     * @var array
33
     */
34
    private $routes;
35
36
    /**
37
     * Base url
38
     *
39
     * @var string
40
     */
41
    protected $baseUrl;
42
43
    /**
44
     * RouteRepository constructor.
45
     *
46
     * @param string $baseUrl
47
     * @param array  $routes [name => definition]
48
     */
49 6
    public function __construct(string $baseUrl, array $routes)
50
    {
51 6
        $this->baseUrl = $baseUrl;
52 6
        $this->routes  = $routes;
53 6
    }
54
55
    /**
56
     * Generate URL by route
57
     *
58
     * @param  string $name       Route name
59
     * @param  array  $parameters Request parameters
60
     * @return string
61
     */
62 4
    public function generate(string $name, array $parameters = []): string
63
    {
64 4
        return $this->getUrlGenerator()->generate($name, $parameters);
65
    }
66
67
    /**
68
     * Get URL generator
69
     *
70
     * @return UrlGeneratorInterface
71
     */
72 4
    protected function getUrlGenerator(): UrlGeneratorInterface
73
    {
74 4
        if ($this->urlGenerator === null) {
75 4
            $this->urlGenerator = new UrlGenerator(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\R...ontext($this->baseUrl)) of type object<Symfony\Component...Generator\UrlGenerator> is incompatible with the declared type object<Generator\UrlGeneratorInterface> of property $urlGenerator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76 4
                $this->assembleRoutesCollection(),
77 4
                new RequestContext($this->baseUrl)
78
            );
79
        }
80
81 4
        return $this->urlGenerator;
82
    }
83
84
    /**
85
     * Assemble routes collection
86
     *
87
     * @return RouteCollection
88
     */
89 4
    protected function assembleRoutesCollection(): RouteCollection
90
    {
91 4
        $collection = new RouteCollection();
92
93 4
        foreach ($this->routes as $name => $definition)
94
        {
95 4
            $route = new Route($definition['path']);
96 4
            $route->setMethods($definition['methods']);
97
98 4
            $collection->add($name, $route);
99
        }
100
101 4
        return $collection;
102
    }
103
}