Test Failed
Push — master ( d4b731...813def )
by Dan
08:06
created

Router   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 71.05%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 134
ccs 27
cts 38
cp 0.7105
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getRouterResponse() 0 16 3
A getRouterResponseFromPath() 0 8 1
A getAdaptor() 0 4 1
A withAdaptor() 0 6 1
A getCollection() 0 7 2
A mergeCollection() 0 6 1
A withCollection() 0 6 1
A isCached() 0 4 1
A disableTrailingSlash() 0 3 1
1
<?php
2
/**
3
 * @file
4
 * This file is part of the RS Framework.
5
 *
6
 * (c) Dan Smith <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Ds\Router;
12
13
use Psr\Http\Message\ServerRequestInterface;
14
use Ds\Router\Interfaces\AdaptorInterface;
15
use Ds\Router\Interfaces\RouteCollectionInterface;
16
use Ds\Router\Interfaces\RouterInterface;
17
18
/**
19
 * Routing Class used to dispatch handlers from ServerRequestInterface.
20
 *
21
 * @package Rs\Router
22
 * @author  Dan Smith    <[email protected]>
23
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link    https://red-sqr.co.uk/Framework/Router/wikis/
25
 */
26
class Router implements RouterInterface
27
{
28
29
    private $ignoreTrailingSlash = false;
30
31
    /**
32
     * @var AdaptorInterface $adaptor   Router Adaptor.
33
     */
34
    protected $adaptor;
35
36
    /**
37
     * @var RouteCollectionInterface $collection    Route Collection
38
     */
39
    protected $collection;
40
41
    /**
42
     * Router constructor.
43
     *
44
     * @param AdaptorInterface         $adaptor    Router Adaptor
45
     * @param RouteCollectionInterface $collection Route Collection
46
     * @param array $options Router Options
47
     */
48 11
    public function __construct(
49
        AdaptorInterface $adaptor,
50
        RouteCollectionInterface $collection,
51
        array $options = []
52
    ) {
53 11
        $this->adaptor = $adaptor;
54 11
        $this->collection = $collection;
55
56 11
        if (isset($options['ignoreTrailingSlash'])){
57
            $this->ignoreTrailingSlash = (bool)$options['ignoreTrailingSlash'];
58
        }
59
60 11
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getRouterResponse(ServerRequestInterface $request)
66
    {
67
68
        $requestUri = $request->getUri()->getPath();
69
70
71
        if ($this->ignoreTrailingSlash === true && $requestUri !== '/'){
72
            $requestUri = rtrim($requestUri,'/');
73
        }
74
75
        return $this->adaptor->match(
76
            $this->collection,
77
            $request->getMethod(),
78
            $requestUri
79
        );
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 2
    public function getRouterResponseFromPath(string $method, string $requestTarget)
86
    {
87 2
        return $this->adaptor->match(
88 2
            $this->collection,
89
            $method,
90
            $requestTarget
91
        );
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 4
    public function getAdaptor()
98
    {
99 4
        return $this->adaptor;
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 1
    public function withAdaptor(AdaptorInterface $adaptor)
106
    {
107 1
        $new = clone $this;
108 1
        $new->adaptor = $adaptor;
109 1
        return $new;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     * @throws \Rs\Router\Exceptions\AdaptorException
115
     */
116 4
    public function getCollection()
117
    {
118 4
        if ($this->adaptor->isCached()) {
119 1
            return $this->adaptor->getCachedRoutes();
120
        }
121 3
        return $this->collection;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     * @throws \Rs\Router\Exceptions\RouteException
127
     */
128 1
    public function mergeCollection(RouteCollectionInterface $collection)
129
    {
130 1
        return $this->withCollection(
131 1
            $this->collection->mergeCollection($collection)
132
        );
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138 2
    public function withCollection(RouteCollectionInterface $collection)
139
    {
140 2
        $new = clone $this;
141 2
        $new->collection = $collection;
142 2
        return $new;
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148 1
    public function isCached()
149
    {
150 1
        return $this->adaptor->isCached();
151
    }
152
153
    /**
154
     * @param bool $ignore
155
     */
156
    public function disableTrailingSlash($ignore = true){
157
        $this->ignoreTrailingSlash = $ignore;
158
    }
159
}
160