RoutesWithoutGate::handle()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Fireworkweb\Gates\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Routing\Route;
7
use Illuminate\Routing\Router;
8
9
class RoutesWithoutGate extends Command
10
{
11
    protected $signature = 'gates:routes-without-gate
12
                            {middleware=gate : The middleware name.}';
13
14
    protected $description = 'Shows routes without gate that has the middleware.';
15
16
    public function handle(Router $router)
17
    {
18
        $routes = $router->getRoutes();
19
20
        $routesWithoutGate = collect($routes)
21
            ->filter(function ($route) {
22
                return $this->hasMiddleware($route) && ! $this->hasGate($route);
23
            })
24
            ->map(function ($route) {
25
                return [$route->getName() ?: $route->getPrefix()];
26
            });
27
28
        if ($routesWithoutGate->isEmpty()) {
29
            $this->info('Great job, no routes without gate. :)');
30
31
            return 0;
32
        } else {
33
            $this->error('You got routes without gate, see list below:');
34
            $this->table(['Route'], $routesWithoutGate->all());
35
36
            return 1;
37
        }
38
    }
39
40
    protected function hasMiddleware(Route $route)
41
    {
42
        return in_array($this->argument('middleware'), $route->middleware());
43
    }
44
45
    protected function hasGate(Route $route)
46
    {
47
        return gate()->has($route->getName());
48
    }
49
}
50