Completed
Push — master ( 7fc1f9...0b10de )
by Mark
13s queued 11s
created

PartialParts::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace DigiFactory\PartialDown\Commands;
4
5
use Closure;
6
use Illuminate\Console\Command;
7
use Illuminate\Routing\Route;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Str;
10
11
class PartialParts extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'partial-parts';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'List all registered routes';
26
27
    /**
28
     * The router instance.
29
     *
30
     * @var \Illuminate\Routing\Router
31
     */
32
    protected $router;
33
34
    /**
35
     * The table headers for the command.
36
     *
37
     * @var array
38
     */
39
    protected $headers = ['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware'];
40
41
    /**
42
     * The columns to display when using the "compact" flag.
43
     *
44
     * @var array
45
     */
46
    protected $compactColumns = ['method', 'uri', 'action'];
47
48
    /**
49
     * Create a new route command instance.
50
     *
51
     * @param \Illuminate\Routing\Router $router
52
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
     */
54
    public function __construct(Router $router)
55
    {
56
        parent::__construct();
57
58
        $this->router = $router;
59
    }
60
61
    /**
62
     * Execute the console command.
63
     *
64
     * @return void
65
     */
66
    public function handle()
67
    {
68
        $parts = collect($this->router->getRoutes())->map(function ($route) {
69
            return [$this->getPartialPartsMiddleware($route)];
70
        })->filter()->unique();
71
72
        if ($parts->count() === 0) {
73
            $this->error('No parts found!');
74
        } else {
75
            $headers = ['Parts in use'];
76
            $this->table($headers, $parts->toArray());
77
        }
78
    }
79
80
    /**
81
     * Get the route information for a given route.
82
     *
83
     * @param \Illuminate\Routing\Route $route
84
     * @return array
85
     */
86
    protected function getPartialPartsMiddleware(Route $route)
87
    {
88
        return collect($route->gatherMiddleware())->map(function ($middleware) {
89
            return $middleware instanceof Closure ? 'Closure' : $middleware;
90
        })->filter(function ($middleware) {
91
            return Str::contains($middleware, 'partialDown');
92
        })->map(function ($middleware) {
93
            return Str::replaceFirst('partialDown:', '', $middleware);
94
        })->first();
95
    }
96
}
97