Passed
Push — master ( fa997c...11836a )
by Carsten
03:39 queued 10s
created

RouteNameUrlCallable::__invoke()   C

Complexity

Conditions 14
Paths 144

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 14.1691

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 19
cts 21
cp 0.9048
rs 5.9
c 0
b 0
f 0
cc 14
nc 144
nop 3
crap 14.1691

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Germania\RouteNameUrlCallable;
3
4
use Slim\Psr7\Request as SlimRequest;
5
use Slim\Psr7\Uri as SlimUri;
6
use Slim\Routing\RouteContext;
7
use Slim\Routing\RouteParser;
8
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\UriInterface;
11
12
class RouteNameUrlCallable
13
{
14
15
16
    /**
17
     * @var SlimRequest
18
     */
19
    public $request;
20
21
    /**
22
     * @var Psr\Http\Message\UriInterface;
23
     */
24
    public $uri;
25
26
27
    /**
28
     * @var Slim\Interfaces\RouteParser
29
     */
30
    public $route_parser;
31
32
33
    /**
34
     * @param SlimRequest $request [description]
35
     */
36 54
    public function __construct( SlimRequest $request )
37
    {
38 54
        $this->request = $request;
39 54
        $this->uri = $request->getUri();
40
41
        
42
        # Method 1
43 54
        $routeContext = RouteContext::fromRequest($request);
44 54
        $this->route_parser = $routeContext->getRouteParser();
45
        
46
        # Method 2
47
        // $this->route_parser = $request->getAttribute('routeParser');
48 54
    }
49
50
51
52
    /**
53
     * @param  string|array $route Route name or array with name, arguments and query parameters
54
     * @param  array  $args   Optional array with URL arguments
55
     * @param  array  $params Optional array with query string parameters
56
     * 
57
     * @return Psr\Http\Message\UriInterface
58
     * @return Slim\Http\Uri Full URI in SLim flavour
59
     */
60 54
    public function __invoke( $route, $args = array(), $params = array() ) : SlimUri
61
    {
62 54
        if (is_null( $args)):
63 6
            $args = array();
64
        endif;
65
66 54
        if (is_null( $params)):
67 6
            $params = array();
68
        endif;
69
70 54
        if (is_string($route)):
71 33
            $name = $route;
72
73 21
        elseif (is_array($route)):
74 12
            $name   = isset($route['name'])   ? $route['name'] : null;
75 12
            $args   = isset($route['args'])   ? array_merge($route['args'], $args) : $args;
76 12
            $params = isset($route['params']) ? array_merge($route['params'], $params) : $params;
77
78 9
        elseif (is_object($route)):
79 9
            $name   = isset($route->name)   ? $route->name : null;
80 9
            $args   = isset($route->args)   ? array_merge( (array) $route->args, $args)   : $args;
81 9
            $params = isset($route->params) ? array_merge( (array) $route->params, $params) : $params;
82
        endif;
83
84 54
        if (empty($name) or !is_string( $name )):
85
            throw new \InvalidArgumentException("Route must be either a) non-empty string with route name"
86
              . " or b) array with keys 'name' and, optionally, 'args' and/or 'params' array");
87
        endif;
88
89
90
        // Get URL path and build GET parameters
91 54
        $url_path = $this->route_parser->urlFor($name, $args);
92 27
        $query_string = http_build_query($params);
93
94
        // Create return value
95 27
        return $this->request->getUri()->withPath( $url_path )->withQuery( $query_string );
96
    }
97
98
}
99