Completed
Push — master ( 2d24bc...d92872 )
by Joschi
03:47
created

AuraRouterAdapter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

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

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 6
    public function __construct(RouterContainer $routerContainer)
72
    {
73 6
        $this->routerContainer = $routerContainer;
74
75
        // Override the default rules
76
        /** @var Rule\RuleIterator $ruleIterator */
77 6
        $ruleIterator = $routerContainer->getRuleIterator();
78 6
        $ruleIterator->set([
79 6
            Kernel::create(Rule\Secure::class),
80 6
            Kernel::create(Rule\Host::class),
81 6
            Kernel::create(Rule\Allows::class),
82 6
            Kernel::create(Rule\Accepts::class),
83 6
            Kernel::create(Authentication::class),
84 6
            Kernel::create(
85 6
                ObjectPath::class,
86 6
                [rtrim(parse_url(getenv('APPARAT_BASE_URL'), PHP_URL_PATH), '/') ?: null]
87 6
            ),
88 6
        ]);
89 6
    }
90
91
    /**
92
     * Register a route
93
     *
94
     * @param RouteInterface $route Route
95
     * @return RouterContainerInterface Self reference
96
     */
97 6
    public function registerRoute(RouteInterface $route)
98
    {
99
        /** @var AuraRoute $auraRoute */
100 6
        $auraRoute = Kernel::create($route->isObject() ? AuraObjectRoute::class : AuraRoute::class);
101 6
        $auraRoute->name($route->getName())
102 6
            ->path($route->getPath())
103 6
            ->allows($route->getVerbs())
104 6
            ->handler($route->getAction())
105 6
            ->tokens($route->getTokens())
106 6
            ->defaults($route->getDefaults())
107 6
            ->wildcard($route->getWildcard())
108 6
            ->host($route->getHost())
109 6
            ->accepts($route->getAccepts())
110 6
            ->auth($route->getAuth())
111 6
            ->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 6
            ->extras($route->getExtras());
113 6
        $this->routerContainer->getMap()->addRoute($auraRoute);
114
115 6
        return $this;
116
    }
117
118
    /**
119
     * Dispatch a request to a route
120
     *
121
     * @param ServerRequestInterface $request
122
     * @return AbstractActionRoute $route
123
     */
124 80
    public function dispatchRequestToRoute(ServerRequestInterface $request)
125
    {
126
        // If no route has been registered
127 80
        if (!count($this->routerContainer->getMap()->getIterator())) {
128 1
            return AuraErrorRoute::cast(null);
129
        }
130
131 79
        $matcher = $this->routerContainer->getMatcher();
132 79
        $route = $matcher->match($request);
133
134
        // If a matching route was found
135 79
        if ($route instanceof ActionRouteInterface) {
136 75
            return $route;
137
        }
138
139
        // Else create an error route
140 4
        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 80
    public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route)
151
    {
152
        // Pre-process the matched attributes
153 80
        $route->preprocessAttributes();
154
155
        // Copy all route attributes to the server request
156 80
        foreach ($route->attributes as $key => $val) {
157 78
            $request = $request->withAttribute($key, $val);
158 80
        }
159
160
        /** @var AbstractActionRoute $route */
161 80
        $handlerParameter = null;
162 80
        $handler = $route->getHandler($handlerParameter);
163
164
        // If the handler is a callable
165 79
        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 78
        $action = Kernel::create($handler, [$request]);
175 78
        return $action->setParams((array)$handlerParameter);
176
    }
177
}
178