PartialParts   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPartialPartsMiddleware() 0 10 2
A __construct() 0 6 1
A handle() 0 15 2
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()->map(function ($route) {
71
            return [$route];
72
        });
73
74
        if ($parts->count() === 0) {
75
            $this->error('No parts found!');
76
        } else {
77
            $headers = ['Parts in use'];
78
            $this->table($headers, $parts->toArray());
79
        }
80
    }
81
82
    /**
83
     * Get the route information for a given route.
84
     *
85
     * @param \Illuminate\Routing\Route $route
86
     * @return array
87
     */
88
    protected function getPartialPartsMiddleware(Route $route)
89
    {
90
        return collect($route->gatherMiddleware())->map(function ($middleware) {
91
            return $middleware instanceof Closure ? 'Closure' : $middleware;
92
        })->filter(function ($middleware) {
93
            return Str::contains($middleware, 'partialDown');
94
        })->map(function ($middleware) {
95
            return Str::replaceFirst('partialDown:', '', $middleware);
96
        })->first();
97
    }
98
}
99