RouterCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 73
rs 10
c 1
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoutes() 0 5 1
A isBlocked() 0 4 3
A isMiddlewared() 0 4 3
A configure() 0 4 1
A execute() 0 37 5
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\Router\Command;
6
7
use Bone\Http\RouterInterface;
8
use Bone\Router\Router;
9
use League\Route\Route;
10
use League\Route\RouteGroup;
11
use League\Route\Router as LeagueRouter;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use ReflectionClass;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use function usort;
21
22
class RouterCommand extends Command
23
{
24
25
    public function __construct(
26
        private readonly Router $router,
27
        private readonly array $blockedRoutes,
28
        private readonly array $routeMiddleware
29
    ) {
30
        parent::__construct('router:list');
31
    }
32
33
    public function configure(): void
34
    {
35
        $this->setDescription('Lists all routes registered with the router');
36
        $this->setHelp('List all routes');
37
    }
38
39
    public function execute(InputInterface $input, OutputInterface $output): int
40
    {
41
        $io = new SymfonyStyle($input, $output);
42
        $io->title('Configured routes :');
43
        $groups = $this->router->getGroups();
44
45
        foreach ($groups as $group) {
46
            $this->getRoutes($group);
47
        }
48
49
        $routes = $this->router->getRoutes();
50
        $sort = function (Route $a, Route $b) {
51
            return $a->getPath() <=> $b->getPath();
52
        };
53
        usort($routes, $sort);
54
        $paths = [];
55
56
        foreach ($routes as $route) {
57
            $path = $route->getPath();
58
            $method = $route->getMethod();
59
            $blocked = $this->isBlocked($method, $path);
60
            $middlewared = $this->isMiddlewared($method, $path);
61
62
            if ($blocked) {
63
               $path = '<fg=red>' .$path . '</>';
64
            }
65
66
            if ($middlewared) {
67
               $path = '<fg=magenta>' .$path . '</>';
68
            }
69
70
            $paths[] = [$method, $path];
71
        }
72
73
        $io->table(['Method', 'Path'], $paths);
74
75
        return Command::SUCCESS;
76
    }
77
78
    private function isMiddlewared(string $method, string $path): bool
79
    {
80
        return in_array($path, $this->routeMiddleware)
81
            || (isset($this->routeMiddleware[$method]) && array_key_exists($path, $this->routeMiddleware[$method]));
82
    }
83
84
    private function isBlocked(string $method, string $path): bool
85
    {
86
        return in_array($path, $this->blockedRoutes)
87
            || (isset($this->blockedRoutes[$method]) && in_array($path, $this->blockedRoutes[$method]));
88
    }
89
90
    private function getRoutes(RouteGroup $group)
91
    {
92
        $mirror = new ReflectionClass(RouteGroup::class);
93
        $callback = $mirror->getProperty('callback')->getValue($group);
94
        $callback($group);
95
    }
96
}
97