AuraRouterAdapter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 121
ccs 53
cts 53
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A registerRoute() 0 20 2
A dispatchRequestToRoute() 0 18 3
B getRouteAction() 0 27 3
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Infrastructure\Route;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Server\Domain\Contract\ActionRouteInterface;
41
use Apparat\Server\Domain\Contract\RouteInterface;
42
use Apparat\Server\Domain\Contract\RouterContainerInterface;
43
use Apparat\Server\Infrastructure\Rule\Authentication;
44
use Apparat\Server\Infrastructure\Rule\ObjectPath;
45
use Apparat\Server\Ports\Action\ActionInterface;
46
use Aura\Router\RouterContainer;
47
use Aura\Router\Rule;
48
use Psr\Http\Message\ResponseInterface;
49
use Psr\Http\Message\ServerRequestInterface;
50
51
/**
52
 * Aura.Router adapter
53
 *
54
 * @package Apparat\Server
55
 * @subpackage Apparat\Server\Infrastructure
56
 */
57
class AuraRouterAdapter implements RouterContainerInterface
58
{
59
    /**
60
     * Aura.Router container
61
     *
62
     * @var RouterContainer
63
     */
64
    protected $routerContainer;
65
66
    /**
67
     * Constructor
68
     *
69
     * @param RouterContainer $routerContainer Router container
70
     */
71 8
    public function __construct(RouterContainer $routerContainer)
72
    {
73 8
        $this->routerContainer = $routerContainer;
74
75
        // Override the default rules
76
        /** @var Rule\RuleIterator $ruleIterator */
77 8
        $ruleIterator = $routerContainer->getRuleIterator();
78 8
        $ruleIterator->set([
79 8
            Kernel::create(Rule\Secure::class),
80 8
            Kernel::create(Rule\Host::class),
81 8
            Kernel::create(Rule\Allows::class),
82 8
            Kernel::create(Rule\Accepts::class),
83 8
            Kernel::create(Authentication::class),
84 8
            Kernel::create(
85 8
                ObjectPath::class,
86 8
                [rtrim(parse_url(getenv('APPARAT_BASE_URL'), PHP_URL_PATH), '/') ?: null]
87 8
            ),
88 8
        ]);
89 8
    }
90
91
    /**
92
     * Register a route
93
     *
94
     * @param RouteInterface $route Route
95
     * @return RouterContainerInterface Self reference
96
     */
97 8
    public function registerRoute(RouteInterface $route)
98
    {
99
        /** @var AuraRoute $auraRoute */
100 8
        $auraRoute = Kernel::create($route->isObject() ? AuraObjectRoute::class : AuraRoute::class);
101 8
        $auraRoute->name($route->getName())
102 8
            ->path($route->getPath())
103 8
            ->allows($route->getVerbs())
104 8
            ->handler($route->getAction())
105 8
            ->tokens($route->getTokens())
106 8
            ->defaults($route->getDefaults())
107 8
            ->wildcard($route->getWildcard())
108 8
            ->host($route->getHost())
109 8
            ->accepts($route->getAccepts())
110 8
            ->auth($route->getAuth())
111 8
            ->secure($route->getSecure())
0 ignored issues
show
Bug introduced by
It seems like $route->getSecure() targeting Apparat\Server\Domain\Co...eInterface::getSecure() can also be of type null; however, Aura\Router\Route::secure() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
112 8
            ->extras($route->getExtras());
113 8
        $this->routerContainer->getMap()->addRoute($auraRoute);
114
115 8
        return $this;
116
    }
117
118
    /**
119
     * Dispatch a request to a route
120
     *
121
     * @param ServerRequestInterface $request
122
     * @return AbstractActionRoute $route
123
     */
124 82
    public function dispatchRequestToRoute(ServerRequestInterface $request)
125
    {
126
        // If no route has been registered
127 82
        if (!count($this->routerContainer->getMap()->getIterator())) {
128 1
            return AuraErrorRoute::cast(null);
129
        }
130
131 81
        $matcher = $this->routerContainer->getMatcher();
132 81
        $route = $matcher->match($request);
133
134
        // If a matching route was found
135 80
        if ($route instanceof ActionRouteInterface) {
136 76
            return $route;
137
        }
138
139
        // Else create an error route
140 5
        return AuraErrorRoute::cast($matcher->getFailedRoute());
141
    }
142
143
    /**
144
     * Prepare and return a route action
145
     *
146
     * @param ServerRequestInterface $request Request
147
     * @param AbstractActionRoute $route Route
148
     * @return ActionInterface|Callable $action Action
149
     */
150 81
    public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route)
151
    {
152
        // Pre-process the matched attributes
153 81
        $route->preprocessAttributes();
154
155
        // Copy all route attributes to the server request
156 81
        foreach ($route->attributes as $key => $val) {
157 79
            $request = $request->withAttribute($key, $val);
158 81
        }
159
160
        /** @var AbstractActionRoute $route */
161 81
        $handlerParameter = null;
162 81
        $handler = $route->getHandler($handlerParameter);
163
164
        // If the handler is a callable
165 80
        if (is_callable($handler)) {
166 1
            return function () use ($handler, $request) {
167
                /** @var ResponseInterface $response */
168 1
                $response = $handler($request);
169 1
                return $response;
170 1
            };
171
        }
172
173
        /** @var ActionInterface $action */
174 79
        $action = Kernel::create($handler, [$request]);
175 79
        return $action->setParams((array)$handlerParameter);
176
    }
177
}
178