Passed
Pull Request — main (#6)
by Chema
02:26
created

Routing::configure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
/**
8
 * @method static get(string $path, object|string $controller, string $action = '__invoke')
9
 * @method static head(string $path, object|string $controller, string $action = '__invoke')
10
 * @method static connect(string $path, object|string $controller, string $action = '__invoke')
11
 * @method static post(string $path, object|string $controller, string $action = '__invoke')
12
 * @method static delete(string $path, object|string $controller, string $action = '__invoke')
13
 * @method static options(string $path, object|string $controller, string $action = '__invoke')
14
 * @method static patch(string $path, object|string $controller, string $action = '__invoke')
15
 * @method static put(string $path, object|string $controller, string $action = '__invoke')
16
 * @method static trace(string $path, object|string $controller, string $action = '__invoke')
17
 */
18
final class Routing
19
{
20
    /**
21
     * @param callable(RoutingConfigurator):void $fn
22
     */
23 73
    public static function configure(callable $fn): void
24
    {
25 73
        $routingConfigurator = new RoutingConfigurator();
26 73
        $fn($routingConfigurator);
27
28 72
        $route = self::findRoute($routingConfigurator);
29
30 72
        if ($route) {
31 65
            echo $route->run($routingConfigurator);
32
        }
33
    }
34
35 72
    private static function findRoute(RoutingConfigurator $routingConfigurator): ?Route
36
    {
37 72
        foreach ($routingConfigurator->routes() as $route) {
38 72
            if ($route->requestMatches()) {
39 65
                return $route;
40
            }
41
        }
42
43 7
        return null;
44
    }
45
}
46