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

MiddlewareFinder::find()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 5
nop 1
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
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