RoutingBusinessFactory::getRouteCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
4
namespace Xervice\Routing\Business;
5
6
7
use DataProvider\RoutingContextDataProvider;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\Matcher\UrlMatcher;
10
use Symfony\Component\Routing\RequestContext;
11
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
12
use Xervice\Core\Business\Model\Factory\AbstractBusinessFactory;
13
use Xervice\Routing\Business\Model\Matcher\MatchProvider;
14
use Xervice\Routing\Business\Model\Matcher\MatchProviderInterface;
15
use Xervice\Routing\Business\Model\Route\RouteCollection;
16
use Xervice\Routing\Business\Model\Route\RouteCollectionInterface;
17
18
/**
19
 * @method \Xervice\Routing\RoutingConfig getConfig()
20
 */
21
class RoutingBusinessFactory extends AbstractBusinessFactory implements RoutingBusinessFactoryInterface
22
{
23
    /**
24
     * @var \Xervice\Routing\Business\Model\Route\RouteCollectionInterface
25
     */
26
    private $routeCollection;
27
28
    /**
29
     * @param \Symfony\Component\HttpFoundation\Request|null $request
30
     *
31
     * @return \Xervice\Routing\Business\Model\Matcher\MatchProviderInterface
32
     */
33 3
    public function createMatcher(Request $request = null): MatchProviderInterface
34
    {
35 3
        return new MatchProvider(
36 3
            $this->createUrlMatcher(),
37 3
            $request ?? $this->createRequest()
38
        );
39
    }
40
41
    /**
42
     * @return \Symfony\Component\Routing\Matcher\UrlMatcher
43
     */
44 3
    public function createUrlMatcher(): UrlMatcher
45
    {
46 3
        return new UrlMatcher(
47 3
            $this->getRouteCollection()->getRouteCollection(),
48 3
            $this->createRequestContext()
49
        );
50
    }
51
52
    /**
53
     * @return \Symfony\Component\Routing\RequestContext
54
     */
55 3
    public function createRequestContext(): RequestContext
56
    {
57 3
        $context = new RequestContext();
58 3
        $context->fromRequest(
59 3
            $this->createRequest()
60
        );
61
62 3
        return $context;
63
    }
64
65
    /**
66
     * @return \Symfony\Component\HttpFoundation\Request
67
     */
68 3
    public function createRequest(): Request
69
    {
70 3
        return Request::createFromGlobals();
71
    }
72
73
    /**
74
     * @return \DataProvider\RoutingContextDataProvider
75
     */
76
    public function createContext(): RoutingContextDataProvider
77
    {
78
        return (new RoutingContextDataProvider())->setContext(
79
            $this->getConfig()->getContext()
80
        );
81
    }
82
83
    /**
84
     * @return \Xervice\Routing\Business\Model\Route\RouteCollectionInterface
85
     */
86 1
    public function createRouteCollection(): RouteCollectionInterface
87
    {
88 1
        return new RouteCollection(
89 1
            new SymfonyRouteCollection()
90
        );
91
    }
92
93
    /**
94
     * @return \Xervice\Routing\Business\Model\Route\RouteCollectionInterface
95
     */
96 3
    public function getRouteCollection(): RouteCollectionInterface
97
    {
98 3
        if ($this->routeCollection === null) {
99 1
            $this->routeCollection = $this->createRouteCollection();
100
        }
101
102 3
        return $this->routeCollection;
103
    }
104
}