Completed
Push — master ( 819036...d88ff8 )
by Joschi
08:13
created

AuraRouterAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\RouteInterface;
41
use Apparat\Server\Domain\Contract\RouterContainerInterface;
42
use Apparat\Server\Ports\Action\ActionInterface;
43
use Aura\Router\Matcher;
44
use Aura\Router\Route;
45
use Aura\Router\RouterContainer;
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 2
    public function __construct(RouterContainer $routerContainer)
70
    {
71 2
        $this->routerContainer = $routerContainer;
72 2
    }
73
74
    /**
75
     * Register a route
76
     *
77
     * @param RouteInterface $route Route
78
     * @return RouterContainerInterface Self reference
79
     */
80 2
    public function registerRoute(RouteInterface $route)
81
    {
82 2
        $auraRoute = $route->isDefault() ?
83 2
            $this->createDefaultRoute($route) :
84 2
            $this->routerContainer->getMap()->route($route->getName(), $route->getPath(), $route->getAction());
85
86 2
        $auraRoute->allows($route->getVerbs())
87 2
            ->tokens($route->getTokens())
88 2
            ->defaults($route->getDefaults())
89 2
            ->wildcard($route->getWildcard())
90 2
            ->host($route->getHost())
91 2
            ->accepts($route->getAccepts())
92 2
            ->auth($route->getAuth())
93 2
            ->secure($route->getSecure())
94 2
            ->extras($route->getExtras());
95 2
        return $this;
96
    }
97
98
    /**
99
     * Create a default route
100
     *
101
     * @param RouteInterface $route Route
102
     * @return AuraDefaultRoute Default route
103
     */
104 1
    protected function createDefaultRoute(RouteInterface $route)
105
    {
106
        /** @var AuraDefaultRoute $auraRoute */
107 1
        $auraRoute = Kernel::create(AuraDefaultRoute::class);
108 1
        $auraRoute->name($route->getName())->path($route->getPath())->handler($route->getAction());
109 1
        $this->routerContainer->getMap()->addRoute($auraRoute);
110 1
        return $auraRoute;
111
    }
112
113
    /**
114
     * Dispatch a request
115
     *
116
     * @param ServerRequestInterface $request
117
     * @return ResponseInterface $response
118
     */
119 2
    public function dispatchRequest(ServerRequestInterface $request)
120
    {
121 2
        $matcher = $this->routerContainer->getMatcher();
122 2
        $route = $matcher->match($request);
123
124
        // If a registered Route could be matched
125 2
        if ($route instanceof Route) {
126 2
            return $this->handleRequestRoute($request, $route);
127
        }
128
129
        // Handle the request mismatch
130
        return $this->handleRequestMismatch($request, $matcher);
131
    }
132
133
    /**
134
     * Handle a matched route request
135
     *
136
     * @param ServerRequestInterface $request Server request
137
     * @param Route $route Matched route
138
     * @return ResponseInterface $response
139
     */
140 2
    protected function handleRequestRoute(ServerRequestInterface $request, Route $route)
141
    {
142
        // If this is a default route: Preprocess the matched attributes
143 2
        if ($route instanceof AuraDefaultRoute) {
144 1
            $route->preprocessAttributes();
145 1
        }
146
147
        // Copy all route attributes to the server request
148 2
        foreach ($route->attributes as $key => $val) {
149 2
            $request = $request->withAttribute($key, $val);
150 2
        }
151
152 2
        $handler = $route->handler;
153
154
        // If the handler is a callable
155 2
        if (is_callable($handler)) {
156
            return $handler($request);
157
        }
158
159
        /** @var ActionInterface $handler */
160 2
        $handler = Kernel::create($handler, [$request]);
0 ignored issues
show
Documentation introduced by
$handler is of type object<Apparat\Server\Po...Action\ActionInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
161 2
        return $handler();
162
    }
163
164
    /**
165
     * Handle a mismatched request
166
     *
167
     * @param ServerRequestInterface $request Server request
168
     * @param Matcher $matcher Matcher
169
     * @return ResponseInterface Response
170
     */
171
    protected function handleRequestMismatch(ServerRequestInterface $request, Matcher $matcher)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $matcher is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        // TODO Error responder
174
//        // Instantiate a response
175
//        $response = Kernel::create(ResponseInterface::class);
176
//
177
//        // Get the first of the best-available non-matched routes
178
//        $failedRoute = $matcher->getFailedRoute();
179
//
180
//        // Which matching rule failed?
181
//        switch ($failedRoute->failedRule) {
182
//            case 'Aura\Router\Rule\Allows':
183
//                // 405 METHOD NOT ALLOWED
184
//                // Send the $failedRoute->allows as 'Allow:'
185
//                break;
186
//            case 'Aura\Router\Rule\Accepts':
187
//                // 406 NOT ACCEPTABLE
188
//                break;
189
//            default:
190
//                // 404 NOT FOUND
191
//                break;
192
//        }
193
//
194
//        return $response;
195
    }
196
}
197