Passed
Push — master ( 4a62ef...fa5d77 )
by Mike
02:24
created

RoutingFactory::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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