Route::getParentGroup()   A
last analyzed

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