Completed
Push — master ( cd1d0d...6a003f )
by Joschi
02:54
created

AuraRouterAdapter::dispatchRequestToRoute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 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\Ports\Action\ActionInterface;
44
use Aura\Router\RouterContainer;
45
use Aura\Router\Rule;
46
use Psr\Http\Message\ResponseInterface;
47
use Psr\Http\Message\ServerRequestInterface;
48
49
/**
50
 * Aura.Router adapter
51
 *
52
 * @package Apparat\Server
53
 * @subpackage Apparat\Server\Infrastructure
54
 */
55
class AuraRouterAdapter implements RouterContainerInterface
56
{
57
    /**
58
     * Aura.Router container
59
     *
60
     * @var RouterContainer
61
     */
62
    protected $routerContainer;
63
64
    /**
65
     * Constructor
66
     *
67
     * @param RouterContainer $routerContainer Router container
68
     */
69 6
    public function __construct(RouterContainer $routerContainer)
70
    {
71 6
        $this->routerContainer = $routerContainer;
72
73
        // Override the default rules
74
        /** @var Rule\RuleIterator $ruleIterator */
75 6
        $ruleIterator = $routerContainer->getRuleIterator();
76 6
        $ruleIterator->set([
77 6
            new Rule\Secure(),
78 6
            new Rule\Host(),
79 6
            new Rule\Allows(),
80 6
            new Rule\Accepts(),
81 6
            new ObjectPath(rtrim(parse_url(getenv('APPARAT_BASE_URL'), PHP_URL_PATH), '/') ?: null)
82
        ]);
83 6
    }
84
85
    /**
86
     * Register a route
87
     *
88
     * @param RouteInterface $route Route
89
     * @return RouterContainerInterface Self reference
90
     */
91 6
    public function registerRoute(RouteInterface $route)
92
    {
93
        /** @var AuraRoute $auraRoute */
94 6
        $auraRoute = Kernel::create($route->isObject() ? AuraObjectRoute::class : AuraRoute::class);
95 6
        $auraRoute->name($route->getName())
96 6
            ->path($route->getPath())
97 6
            ->allows($route->getVerbs())
98 6
            ->handler($route->getAction())
99 6
            ->tokens($route->getTokens())
100 6
            ->defaults($route->getDefaults())
101 6
            ->wildcard($route->getWildcard())
102 6
            ->host($route->getHost())
103 6
            ->accepts($route->getAccepts())
104 6
            ->auth($route->getAuth())
105 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...
106 6
            ->extras($route->getExtras());
107 6
        $this->routerContainer->getMap()->addRoute($auraRoute);
108
109 6
        return $this;
110
    }
111
112
    /**
113
     * Dispatch a request to a route
114
     *
115
     * @param ServerRequestInterface $request
116
     * @return AbstractActionRoute $route
117
     */
118 80
    public function dispatchRequestToRoute(ServerRequestInterface $request)
119
    {
120
        // If no route has been registered
121 80
        if (!count($this->routerContainer->getMap()->getIterator())) {
122 1
            return AuraErrorRoute::cast(null);
123
        }
124
125 79
        $matcher = $this->routerContainer->getMatcher();
126 79
        $route = $matcher->match($request);
127
128
        // If a matching route was found
129 79
        if ($route instanceof ActionRouteInterface) {
130 75
            return $route;
131
        }
132
133
        // Else create an error route
134 4
        return AuraErrorRoute::cast($matcher->getFailedRoute());
135
    }
136
137
    /**
138
     * Prepare and return a route action
139
     *
140
     * @param ServerRequestInterface $request Request
141
     * @param AbstractActionRoute $route Route
142
     * @return ActionInterface|Callable $action Action
143
     */
144 80
    public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route)
145
    {
146
        // Pre-process the matched attributes
147 80
        $route->preprocessAttributes();
148
149
        // Copy all route attributes to the server request
150 80
        foreach ($route->attributes as $key => $val) {
151 78
            $request = $request->withAttribute($key, $val);
152
        }
153
154
        /** @var AbstractActionRoute $route */
155 80
        $handlerParameter = null;
156 80
        $handler = $route->getHandler($handlerParameter);
157
158
        // If the handler is a callable
159 79
        if (is_callable($handler)) {
160 1
            return function () use ($handler, $request) {
161
                /** @var ResponseInterface $response */
162 1
                $response = $handler($request);
163 1
                return $response;
164 1
            };
165
        }
166
167
        /** @var ActionInterface $action */
168 78
        $action = Kernel::create($handler, [$request]);
169 78
        return $action->setParams((array)$handlerParameter);
170
    }
171
}
172