Completed
Pull Request — master (#178)
by Phil
01:48
created

Route::getCallable()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 40
nop 1
crap 12

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 declare(strict_types=1);
2
3
namespace League\Route;
4
5
use InvalidArgumentException;
6
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
7
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait};
8
use Psr\Container\ContainerInterface;
9
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
10
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
11
12
class Route implements
13
    MiddlewareInterface,
14
    MiddlewareAwareInterface,
15
    RouteConditionHandlerInterface,
16
    StrategyAwareInterface
17
{
18
    use MiddlewareAwareTrait;
19
    use RouteConditionHandlerTrait;
20
    use StrategyAwareTrait;
21
22
    /**
23
     * @var callable|string
24
     */
25
    protected $handler;
26
27
    /**
28
     * @var \League\Route\RouteGroup
29
     */
30
    protected $group;
31
32
    /**
33
     * @var string
34
     */
35
    protected $method;
36
37
    /**
38
     * @var string
39
     */
40
    protected $path;
41
42
    /**
43
     * @var array
44
     */
45
    protected $vars = [];
46
47 45
    public function __construct(string $method, string $path, $handler)
48
    {
49 45
        $this->method  = $method;
50 45
        $this->path    = $path;
51 45
        $this->handler = $handler;
52 45
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 12
    public function process(
58
        ServerRequestInterface $request,
59
        RequestHandlerInterface $requestHandler
60
    ) : ResponseInterface {
61 12
        return $this->getStrategy()->invokeRouteCallable($this, $request);
62
    }
63
64
    /**
65
     * Get the callable.
66
     *
67
     * @param ?\Psr\Container\ContainerInterface $container
0 ignored issues
show
Documentation introduced by
The doc-type ?\Psr\Container\ContainerInterface could not be parsed: Unknown type name "?\Psr\Container\ContainerInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
68
     *
69
     * @throws \RuntimeException
70
     *
71
     * @return callable
72
     */
73 33
    public function getCallable(?ContainerInterface $container = null) : callable
74
    {
75 33
        $callable = $this->handler;
76
77 33
        if (is_string($callable) && strpos($callable, '::') !== false) {
78 6
            $callable = explode('::', $callable);
79
        }
80
81 33
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
82 3
            $callable = [$callable[0], $callable[1]];
83
        }
84
85 33
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
86 6
            $class = (! is_null($container) && $container->has($callable[0]))
87 3
                ? $container->get($callable[0])
88 6
                : new $callable[0];
89
90 6
            $callable = [$class, $callable[1]];
91
        }
92
93 33
        if (! is_callable($callable)) {
94 3
            throw new InvalidArgumentException('Could not resolve a callable for this route');
95
        }
96
97 30
        return $callable;
98
    }
99
100
    /**
101
     * Return vars to be passed to route callable.
102
     *
103
     * @return array
104
     */
105 15
    public function getVars() : array
106
    {
107 15
        return $this->vars;
108
    }
109
110
    /**
111
     * Set vars to be passed to route callable.
112
     *
113
     * @param array $vars
114
     *
115
     * @return \League\Route\Route
116
     */
117 15
    public function setVars(array $vars) : self
118
    {
119 15
        $this->vars = $vars;
120
121 15
        return $this;
122
    }
123
124
    /**
125
     * Get the parent group.
126
     *
127
     * @return ?\League\Route\RouteGroup
0 ignored issues
show
Documentation introduced by
The doc-type ?\League\Route\RouteGroup could not be parsed: Unknown type name "?\League\Route\RouteGroup" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
128
     */
129 15
    public function getParentGroup() : ?RouteGroup
130
    {
131 15
        return $this->group;
132
    }
133
134
    /**
135
     * Set the parent group.
136
     *
137
     * @param \League\Route\RouteGroup $group
138
     *
139
     * @return \League\Route\Route
140
     */
141 3
    public function setParentGroup(RouteGroup $group) : self
142
    {
143 3
        $this->group = $group;
144
145 3
        return $this;
146
    }
147
148
    /**
149
     * Get the path.
150
     *
151
     * @return string
152
     */
153 24
    public function getPath() : string
154
    {
155 24
        return $this->path;
156
    }
157
158
    /**
159
     * Get the methods.
160
     *
161
     * @return string
162
     */
163 24
    public function getMethod() : string
164
    {
165 24
        return $this->method;
166
    }
167
}
168