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

MiddlewareFinder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 24
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 10 3
A __construct() 0 3 1
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\Exceptions\PageNotFoundException;
16
use BlitzPHP\Exceptions\RedirectException;
17
use BlitzPHP\Router\Router;
18
19
/**
20
 * Trouve des middlewares.
21
 */
22
final class MiddlewareFinder
23
{
24
    private readonly Router $router;
25
26
    public function __construct(?Router $router = null)
27
    {
28 2
        $this->router = $router ?? Services::router();
0 ignored issues
show
Bug introduced by
The property router is declared read-only in BlitzPHP\Cli\Commands\Routes\MiddlewareFinder.
Loading history...
29
    }
30
31
    /**
32
     * @param string $uri Chemin URI pour trouver des middlewares
33
     *
34
     * @return array Tableau d'alias de middleware ou de nom de classe
35
     */
36
    public function find(string $uri): array
37
    {
38
        try {
39 2
            $this->router->handle($uri);
40
41 2
            return $this->router->getMiddlewares();
42
        } catch (RedirectException) {
43 2
            return [];
44
        } catch (PageNotFoundException) {
45 2
            return ['<unknown>'];
46
        }
47
    }
48
}
49