Completed
Pull Request — master (#283)
by Phil
04:05 queued 40s
created

Route::getCallable()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 7.3166
cc 11
nc 32
nop 1
crap 11

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
3
declare(strict_types=1);
4
5
namespace League\Route;
6
7
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
8
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait, StrategyInterface};
9
use Psr\Container\ContainerInterface;
10
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
11
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
12
use RuntimeException;
13
14
class Route implements
15
    MiddlewareInterface,
16
    MiddlewareAwareInterface,
17
    RouteConditionHandlerInterface,
18
    StrategyAwareInterface
19
{
20
    use MiddlewareAwareTrait;
21
    use RouteConditionHandlerTrait;
22
    use StrategyAwareTrait;
23
24
    /**
25
     * @var callable|string
26
     */
27
    protected $handler;
28
29
    /**
30
     * @var RouteGroup
31
     */
32
    protected $group;
33
34
    /**
35
     * @var string
36
     */
37
    protected $method;
38
39
    /**
40
     * @var string
41
     */
42
    protected $path;
43
44
    /**
45
     * @var array
46
     */
47
    protected $vars = [];
48
49 78
    public function __construct(string $method, string $path, $handler)
50
    {
51 78
        $this->method  = $method;
52 78
        $this->path    = $path;
53 78
        $this->handler = $handler;
54 78
    }
55
56 48
    public function getCallable(?ContainerInterface $container = null): callable
57
    {
58 48
        $callable = $this->handler;
59
60 48
        if (is_string($callable) && strpos($callable, '::') !== false) {
61 6
            $callable = explode('::', $callable);
62
        }
63
64 48
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
0 ignored issues
show
introduced by
The condition is_object($callable[0]) is always false.
Loading history...
65 3
            $callable = [$callable[0], $callable[1]];
66
        }
67
68 48
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
69 6
            $callable = [$this->resolve($callable[0], $container), $callable[1]];
70
        }
71
72 48
        if (is_string($callable)) {
73 3
            $callable = $this->resolve($callable, $container);
74
        }
75
76 48
        if (!is_callable($callable)) {
77 3
            throw new RuntimeException('Could not resolve a callable for this route');
78
        }
79
80 45
        return $callable;
81
    }
82
83 51
    public function getMethod(): string
84
    {
85 51
        return $this->method;
86
    }
87
88 30
    public function getParentGroup(): ?RouteGroup
89
    {
90 30
        return $this->group;
91
    }
92
93 54
    public function getPath(array $replacements = []): string
94
    {
95 54
        $toReplace = [];
96
97 54
        foreach ($replacements as $wildcard => $actual) {
98 3
            $toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual;
99
        }
100
101 54
        return preg_replace(array_keys($toReplace), array_values($toReplace), $this->path);
102
    }
103
104 30
    public function getVars(): array
105
    {
106 30
        return $this->vars;
107
    }
108
109 30
    public function process(
110
        ServerRequestInterface $request,
111
        RequestHandlerInterface $requestHandler
112
    ): ResponseInterface {
113 30
        $strategy = $this->getStrategy();
114
115 30
        if (!($strategy instanceof StrategyInterface)) {
116 3
            throw new RuntimeException('A strategy must be set to process a route');
117
        }
118
119 27
        return $strategy->invokeRouteCallable($this, $request);
120
    }
121
122 12
    public function setParentGroup(RouteGroup $group): self
123
    {
124 12
        $this->group = $group;
125 12
        $prefix      = $this->group->getPrefix();
126 12
        $path        = $this->getPath();
127
128 12
        if (strcmp($prefix, substr($path, 0, strlen($prefix))) !== 0) {
129 3
            $path = $prefix . $path;
130 3
            $this->path = $path;
131
        }
132
133 12
        return $this;
134
    }
135
136 39
    public function setVars(array $vars): self
137
    {
138 39
        $this->vars = $vars;
139 39
        return $this;
140
    }
141
142 9
    protected function resolve(string $class, ?ContainerInterface $container = null)
143
    {
144 9
        if ($container instanceof ContainerInterface && $container->has($class)) {
145 3
            return $container->get($class);
146
        }
147
148 6
        if (class_exists($class)) {
149 3
            return new $class();
150
        }
151
152 3
        return $class;
153
    }
154
}
155