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

MiddlewareCheck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 69
ccs 11
cts 14
cp 0.7856
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 37 4
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\Utilities;
13
14
use BlitzPHP\Cli\Commands\Routes\MiddlewareCollector;
15
use BlitzPHP\Cli\Console\Command;
16
use BlitzPHP\Container\Services;
17
18
/**
19
 * verifie les middleware d'une route.
20
 */
21
class MiddlewareCheck extends Command
22
{
23
    /**
24
     * @var string Groupe
25
     */
26
    protected $group = 'BlitzPHP';
27
28
    /**
29
     * @var string Nom
30
     */
31
    protected $name = 'middleware:check';
32
33
    /**
34
     * @var string Description
35
     */
36
    protected $description = 'Vérifiez les middleware d\'une route.';
37
38
    protected $service = 'Service de configuration';
39
40
    /**
41
     * Arguments de la commande
42
     *
43
     * @var array<string, string>
44
     */
45
    protected $arguments = [
46
        'method' => 'La methode HTTP. get, post, put, etc.',
47
        'route'  => 'La route (chemin d\'URI) pour vérifier les middlewares.',
48
    ];
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function execute(array $params)
54
    {
55 2
        $method = strtolower($this->argument('method', $params[0] ?? ''));
56 2
        $route  = $this->argument('route', $params[1] ?? '');
57
58
        if (empty($route) || $method === '') {
59 2
            $this->fail('Vous devez spécifier un verbe HTTP et une route.')->eol();
60
            $this->write('Exemple: middleware:check get /')->eol();
61
            $this->write('         middleware:check put products/1');
62
63
            return EXIT_ERROR;
64
        }
65
66
        // Chargement des routes
67 2
        Services::routes()->loadRoutes();
68
69 2
        $middlewareCollector = new MiddlewareCollector();
70
71 2
        $middlewares = $middlewareCollector->get($method, $route);
72
73
        // PageNotFoundException
74
        if ($middlewares === ['<unknown>']) {
75 2
            $this->fail('Impossible de trouver une route: ');
76 2
            $this->colorize('"' . strtoupper($method) . ' ' . $route . '"', 'black');
77
78 2
            return EXIT_ERROR;
79
        }
80
81
        $this->table([
82
            [
83
                'Methode'     => strtoupper($method),
84
                'Route'       => $route,
85
                'Middlewares' => implode(' ', $middlewares),
86
            ],
87 2
        ]);
88
89 2
        return EXIT_SUCCESS;
90
    }
91
}
92