Completed
Pull Request — master (#283)
by Phil
22:36
created

Route::getCallable()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 7.3166
c 0
b 0
f 0
ccs 7
cts 7
cp 1
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
    public function __construct(string $method, string $path, $handler)
50
    {
51
        $this->method  = $method;
52
        $this->path    = $path;
53
        $this->handler = $handler;
54
    }
55 72
56
    public function getCallable(?ContainerInterface $container = null): callable
57 72
    {
58 72
        $callable = $this->handler;
59 72
60 72
        if (is_string($callable) && strpos($callable, '::') !== false) {
61
            $callable = explode('::', $callable);
62
        }
63
64
        if (is_array($callable) && isset($callable[0]) && is_object($callable[0])) {
65 24
            $callable = [$callable[0], $callable[1]];
66
        }
67
68
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
69 24
            $callable = [$this->resolve($callable[0], $container), $callable[1]];
70
        }
71
72
        if (is_string($callable)) {
73
            $callable = $this->resolve($callable, $container);
74
        }
75
76
        if (!is_callable($callable)) {
77
            throw new RuntimeException('Could not resolve a callable for this route');
78
        }
79
80
        return $callable;
81 45
    }
82
83 45
    public function getMethod(): string
84
    {
85 45
        return $this->method;
86 6
    }
87
88
    public function getParentGroup(): ?RouteGroup
89 45
    {
90 3
        return $this->group;
91
    }
92
93 45
    public function getPath(array $replacements = []): string
94 6
    {
95
        $toReplace = [];
96
97 45
        foreach ($replacements as $wildcard => $actual) {
98
            $toReplace['/{' . preg_quote($wildcard, '/') . '(:.*?)?}/'] = $actual;
99
        }
100
101 45
        return preg_replace(array_keys($toReplace), array_values($toReplace), $this->path);
102 3
    }
103
104
    public function getVars(): array
105 42
    {
106
        return $this->vars;
107
    }
108
109
    public function process(
110
        ServerRequestInterface $request,
111
        RequestHandlerInterface $requestHandler
112
    ): ResponseInterface {
113
        $strategy = $this->getStrategy();
114
115
        if (!($strategy instanceof StrategyInterface)) {
116 6
            throw new RuntimeException('A strategy must be set to process a route');
117
        }
118 6
119 3
        return $strategy->invokeRouteCallable($this, $request);
120
    }
121
122 3
    public function setParentGroup(RouteGroup $group): self
123
    {
124
        $this->group = $group;
125
        $prefix      = $this->group->getPrefix();
126
        $path        = $this->getPath();
127
128
        if (strcmp($prefix, substr($path, 0, strlen($prefix))) !== 0) {
129
            $path = $prefix . $path;
130 27
            $this->path = $path;
131
        }
132 27
133
        return $this;
134
    }
135
136
    public function setVars(array $vars): self
137
    {
138
        $this->vars = $vars;
139
        return $this;
140
    }
141
142 27
    protected function resolve(string $class, ?ContainerInterface $container = null)
143
    {
144 27
        if ($container instanceof ContainerInterface && $container->has($class)) {
145
            return $container->get($class);
146 27
        }
147
148
        if (class_exists($class)) {
149
            return new $class();
150
        }
151
152
        return $class;
153
    }
154
}
155