Passed
Pull Request — 5.x (#307)
by
unknown
42:08 queued 16:29
created

Route::resolvePath()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 9.4888
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route;
6
7
use FastRoute\RouteParser\Std;
8
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
9
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait, StrategyInterface};
10
use Psr\Container\ContainerInterface;
11
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
12
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
13
use RuntimeException;
14
15
class Route implements
16
    MiddlewareInterface,
17
    MiddlewareAwareInterface,
18
    RouteConditionHandlerInterface,
19
    StrategyAwareInterface
20
{
21
    use MiddlewareAwareTrait;
22
    use RouteConditionHandlerTrait;
23
    use StrategyAwareTrait;
24
25
    /**
26
     * @var callable|string
27
     */
28
    protected $handler;
29
30
    /**
31
     * @var RouteGroup
32
     */
33
    protected $group;
34
35
    /**
36
     * @var string
37
     */
38
    protected $method;
39
40
    /**
41
     * @var string
42
     */
43
    protected $path;
44
45
    /**
46
     * @var array
47
     */
48
    protected $vars = [];
49 81
50
    public function __construct(string $method, string $path, $handler)
51 81
    {
52 81
        $this->method  = $method;
53 81
        $this->path    = $path;
54 81
        $this->handler = $handler;
55
    }
56 48
57
    public function getCallable(?ContainerInterface $container = null): callable
58 48
    {
59
        $callable = $this->handler;
60 48
61 6
        if (is_string($callable) && strpos($callable, '::') !== false) {
62
            $callable = explode('::', $callable);
63
        }
64 48
65 3
        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...
66
            $callable = [$callable[0], $callable[1]];
67
        }
68 48
69 6
        if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
70
            $callable = [$this->resolve($callable[0], $container), $callable[1]];
71
        }
72 48
73 3
        if (is_string($callable)) {
74
            $callable = $this->resolve($callable, $container);
75
        }
76 48
77 3
        if (!is_callable($callable)) {
78
            throw new RuntimeException('Could not resolve a callable for this route');
79
        }
80 45
81
        return $callable;
82
    }
83 51
84
    public function getMethod(): string
85 51
    {
86
        return $this->method;
87
    }
88 30
89
    public function getParentGroup(): ?RouteGroup
90 30
    {
91
        return $this->group;
92
    }
93 57
94
    public function resolvePath(array $replacements = []): string
95 57
    {
96
        $parser = new Std();
97 57
        $routeData = $parser->parse($this->path);
98 3
        $longestPossibleRoute = end($routeData);
99
        $result = [];
100
        foreach ($longestPossibleRoute as $routeSegment) {
101 57
            if (is_string($routeSegment)) {
102
                $result[] = $routeSegment;
103
            } else if (is_array($routeSegment)) {
104 30
                $wildcard = $routeSegment[0];
105
                if (array_key_exists($wildcard, $replacements)) {
106 30
                    $result[] = $replacements[$wildcard];
107
                } else {
108
                    break;
109 30
                }
110
            }
111
        }
112
        return rtrim(implode($result), '/');
113 30
    }
114
115 30
    public function getPath(?array $replacements = null): string
116 3
    {
117
        if ($replacements !== null) {
118
            return $this->resolvePath($replacements);
119 27
        }
120
121
        return $this->path;
122 15
    }
123
124 15
    public function getVars(): array
125 15
    {
126 15
        return $this->vars;
127
    }
128 15
129 3
    public function process(
130 3
        ServerRequestInterface $request,
131
        RequestHandlerInterface $handler
132
    ): ResponseInterface {
133 15
        $strategy = $this->getStrategy();
134
135
        if (!($strategy instanceof StrategyInterface)) {
136 39
            throw new RuntimeException('A strategy must be set to process a route');
137
        }
138 39
139 39
        return $strategy->invokeRouteCallable($this, $request);
140
    }
141
142 9
    public function setParentGroup(RouteGroup $group): self
143
    {
144 9
        $this->group = $group;
145 3
        $prefix      = $this->group->getPrefix();
146
        $path        = $this->getPath();
147
148 6
        if (strcmp($prefix, substr($path, 0, strlen($prefix))) !== 0) {
149 3
            $path = $prefix . $path;
150
            $this->path = $path;
151
        }
152 3
153
        return $this;
154
    }
155
156
    public function setVars(array $vars): self
157
    {
158
        $this->vars = $vars;
159
        return $this;
160
    }
161
162
    protected function resolve(string $class, ?ContainerInterface $container = null)
163
    {
164
        if ($container instanceof ContainerInterface && $container->has($class)) {
165
            return $container->get($class);
166
        }
167
168
        if (class_exists($class)) {
169
            return new $class();
170
        }
171
172
        return $class;
173
    }
174
}
175