Router   A
last analyzed

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