Completed
Push — master ( cc1044...c261ff )
by Joschi
05:08
created

AuraRouterAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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;
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
    public function __construct(RouterContainer $routerContainer)
70
    {
71
        $this->routerContainer = $routerContainer;
72
    }
73
74
    /**
75
     * Register a route
76
     *
77
     * @param RouteInterface $route Route
78
     * @return RouterContainerInterface Self reference
79
     */
80
    public function registerRoute(RouteInterface $route)
81
    {
82
        $this->routerContainer->getMap()
83
            ->route($route->getName(), $route->getPath(), $route->getAction())
84
            ->allows($route->getVerbs())
85
            ->tokens($route->getTokens())
86
            ->defaults($route->getDefaults())
87
            ->wildcard($route->getWildcard())
88
            ->host($route->getHost())
89
            ->accepts($route->getAccepts())
90
            ->auth($route->getAuth())
91
            ->secure($route->getSecure())
92
            ->extras($route->getExtras());
93
        return $this;
94
    }
95
96
    /**
97
     * Dispatch a request
98
     *
99
     * @param ServerRequestInterface $request
100
     * @return ResponseInterface $response
101
     */
102
    public function dispatchRequest(ServerRequestInterface $request)
103
    {
104
        $matcher = $this->routerContainer->getMatcher();
105
        $route = $matcher->match($request);
106
107
        // If a registered Route could be matched
108
        if ($route instanceof Route) {
109
            return $this->handleRequestRoute($request, $route);
110
        }
111
112
        // Handle the request mismatch
113
        return $this->handleRequestMismatch($request, $matcher);
114
    }
115
116
    /**
117
     * Handle a matched route request
118
     *
119
     * @param ServerRequestInterface $request Server request
120
     * @param Route $route Matched route
121
     * @return ResponseInterface $response
122
     */
123
    protected function handleRequestRoute(ServerRequestInterface $request, Route $route) {
124
        // Copy all route attributes to the server request
125
        foreach ($route->attributes as $key => $val) {
126
            $request = $request->withAttribute($key, $val);
127
        }
128
129
        $handler = $route->handler;
130
131
        // If the handler is a callable
132
        if (is_callable($handler)) {
133
            return $handler($request);
134
        }
135
136
        /** @var ActionInterface $handler */
137
        $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...
138
        return $handler();
139
    }
140
141
    /**
142
     * Handle a mismatched request
143
     *
144
     * @param ServerRequestInterface $request Server request
145
     * @param Matcher $matcher Matcher
146
     * @return ResponseInterface Response
147
     */
148
    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...
149
        // TODO Error responder
150
//        // Instantiate a response
151
//        $response = Kernel::create(ResponseInterface::class);
152
//
153
//        // Get the first of the best-available non-matched routes
154
//        $failedRoute = $matcher->getFailedRoute();
155
//
156
//        // Which matching rule failed?
157
//        switch ($failedRoute->failedRule) {
158
//            case 'Aura\Router\Rule\Allows':
159
//                // 405 METHOD NOT ALLOWED
160
//                // Send the $failedRoute->allows as 'Allow:'
161
//                break;
162
//            case 'Aura\Router\Rule\Accepts':
163
//                // 406 NOT ACCEPTABLE
164
//                break;
165
//            default:
166
//                // 404 NOT FOUND
167
//                break;
168
//        }
169
//
170
//        return $response;
171
    }
172
}
173