Passed
Push — 6.x ( 90a58b...9a8fa6 )
by Phil
02:02
created

Route::getVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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