Passed
Push — main ( c6deb1...79ccdf )
by Dimitri
12:23
created

MiddlewareCheck   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 68
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 38 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
    /**
39
     * Arguments de la commande
40
     *
41
     * @var array<string, string>
42
     */
43
    protected $arguments = [
44
        'method' => 'La methode HTTP. get, post, put, etc.',
45
        'route'  => 'La route (chemin d\'URI) pour vérifier les middlewares.',
46
    ];
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function execute(array $params)
52
    {
53
		$method = strtolower($this->argument('method', ''));
54
        $route  = $this->argument('route', '');
55
56
        if (empty($route) || empty($method)) {
57
            $this->fail('Vous devez spécifier un verbe HTTP et une route.')->eol();
58
            $this->write('  Usage: ' . $this->usage)->eol();
59
            $this->write('Exemple: middleware:check get /')->eol();
60
            $this->write('         middleware:check put products/1');
61
62
            return EXIT_ERROR;
63
        }
64
65
        // Chargement des routes
66
        Services::routes()->loadRoutes();
67
68
        $middlewareCollector = new MiddlewareCollector();
69
70
        $middlewares = $middlewareCollector->get($method, $route);
71
72
        // PageNotFoundException
73
        if ($middlewares === ['<unknown>']) {
74
            $this->fail("Impossible de trouver une route: ") .
0 ignored issues
show
Bug introduced by
Are you sure $this->fail('Impossible de trouver une route: ') of type BlitzPHP\Cli\Commands\Utilities\MiddlewareCheck can be used in concatenation? Consider adding a __toString()-method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            /** @scrutinizer ignore-type */ $this->fail("Impossible de trouver une route: ") .
Loading history...
75
			$this->colorize('"' . strtoupper($method) . ' ' . $route . '"', 'black');
76
77
            return EXIT_ERROR;
78
        }
79
80
		$this->table([
81
			[
82
				'Methode' => strtoupper($method),
83
				'Route' => $route,
84
				'Middlewares' => implode(' ', $middlewares),
85
			]
86
		]);
87
88
        return EXIT_SUCCESS;
89
    }
90
}
91