Completed
Push — master ( d88ff8...31f153 )
by Joschi
02:55
created

AuraRouterAdapter::handleRequestMismatch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 1

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 25
ccs 0
cts 2
cp 0
rs 8.8571
cc 1
eloc 1
nc 1
nop 2
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\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\Matcher;
45
use Aura\Router\Route;
46
use Aura\Router\RouterContainer;
47
use Psr\Http\Message\ResponseInterface;
48
use Psr\Http\Message\ServerRequestInterface;
49
50
/**
51
 * Aura.Router adapter
52
 *
53
 * @package Apparat\Server
54
 * @subpackage Apparat\Server\Infrastructure
55
 */
56
class AuraRouterAdapter implements RouterContainerInterface
57
{
58
    /**
59
     * Aura.Router container
60
     *
61
     * @var RouterContainer
62
     */
63
    protected $routerContainer;
64
65
    /**
66
     * Constructor
67
     *
68
     * @param RouterContainer $routerContainer Router container
69
     */
70 2
    public function __construct(RouterContainer $routerContainer)
71
    {
72 2
        $this->routerContainer = $routerContainer;
73 2
    }
74
75
    /**
76
     * Register a route
77
     *
78
     * @param RouteInterface $route Route
79
     * @return RouterContainerInterface Self reference
80
     */
81 2
    public function registerRoute(RouteInterface $route)
82
    {
83
        /** @var AuraRoute $auraRoute */
84 2
        $auraRoute = Kernel::create($route->isDefault() ? AuraDefaultRoute::class : AuraRoute::class);
85 2
        $auraRoute->name($route->getName())
86 2
            ->path($route->getPath())
87 2
            ->handler($route->getAction())
88 2
            ->allows($route->getVerbs())
89 2
            ->tokens($route->getTokens())
90 2
            ->defaults($route->getDefaults())
91 2
            ->wildcard($route->getWildcard())
92 2
            ->host($route->getHost())
93 2
            ->accepts($route->getAccepts())
94 2
            ->auth($route->getAuth())
95 2
            ->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...
96 2
            ->extras($route->getExtras());
97 2
        $this->routerContainer->getMap()->addRoute($auraRoute);
98
99 2
        return $this;
100
    }
101
102
    /**
103
     * Dispatch a request to a route
104
     *
105
     * @param ServerRequestInterface $request
106
     * @return ActionRouteInterface $route
107
     */
108 2
    public function dispatchRequestToRoute(ServerRequestInterface $request)
109
    {
110 2
        $matcher = $this->routerContainer->getMatcher();
111 2
        $route = $matcher->match($request);
112
113
        // If a matching route was found
114 2
        if ($route instanceof ActionRouteInterface) {
115 2
            return $route;
116
        }
117
118
        // Else create an error route
119
        return AuraErrorRoute::cast($matcher->getFailedRoute());
120
    }
121
122
    /**
123
     * Prepare and return a route action
124
     *
125
     * @param ServerRequestInterface $request Request
126
     * @param ActionRouteInterface $route Route
127
     * @return ActionInterface|Callable $action Action
128
     */
129 2
    public function getRouteAction(ServerRequestInterface $request, ActionRouteInterface $route)
130
    {
131
        // Preprocess the matched attributes
132 2
        $route->preprocessAttributes();
133
134
        // Copy all route attributes to the server request
135 2
        foreach ($route->attributes as $key => $val) {
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface Apparat\Server\Domain\Co...ct\ActionRouteInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
136 2
            $request = $request->withAttribute($key, $val);
137 2
        }
138
139
        /** @var AbstractActionRoute $route */
140 2
        $handler = $route->handler;
0 ignored issues
show
Documentation introduced by
The property $handler is declared protected in Aura\Router\Route. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
142
        // If the handler is a callable
143 2
        if (is_callable($handler)) {
144
            return function () use ($handler, $request) {
145
                /** @var ResponseInterface $response */
146
                $response = $handler($request);
147
                return $response;
148
            };
149
        }
150
151 2
        return Kernel::create($handler, [$request]);
152
    }
153
}
154