Passed
Pull Request — main (#27)
by Dimitri
05:06
created

MiddlewareCollector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 39
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get() 0 13 2
A createRouter() 0 9 2
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Routes;
13
14
use BlitzPHP\Container\Services;
15
use BlitzPHP\Http\Request;
16
use BlitzPHP\Router\Router;
17
18
/**
19
 * Collecte les middlewares pour une route
20
 */
21
final class MiddlewareCollector
22
{
23
    /**
24
     * @param bool $resetRoutes Indique s'il faut réinitialiser les routes définies. S'il est défini sur true, les middlewares de routage sont introuvables.
25
     */
26
    public function __construct(private readonly bool $resetRoutes = false)
27
    {
28
    }
29
30
    /**
31
     * @param string $method Methode HTTP
32
     * @param string $uri    Chemin URI pour trouver des middlewares
33
     *
34
     * @return array|array{before: list<string>, after: list<string>} tableau d'alias de middleware ou de nom de classe
0 ignored issues
show
Documentation Bug introduced by
The doc comment array|array{before: list...>, after: list<string>} at position 6 could not be parsed: Expected '}' at position 6, but found 'list'.
Loading history...
35
     */
36
    public function get(string $method, string $uri): array
37
    {
38
        if ($method === 'cli') {
39 2
            return [];
40
        }
41
42 2
        $request = Services::request(false)->withMethod($method);
43
44 2
        $router = $this->createRouter($request);
45
46 2
        $finder = new MiddlewareFinder($router);
47
48 2
        return $finder->find($uri);
49
    }
50
51
    private function createRouter(Request $request): Router
52
    {
53 2
        $routes = Services::routes();
54
55
        if ($this->resetRoutes) {
56 2
            $routes->resetRoutes();
57
        }
58
59 2
        return new Router($routes, $request);
60
    }
61
}
62