Completed
Pull Request — master (#4)
by Mark
03:43
created

PartialParts   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 13 2
A getPartialPartsMiddleware() 0 10 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\Arr;
10
use Illuminate\Support\Str;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class PartialParts extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'partial-parts';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'List all registered routes';
28
29
    /**
30
     * The router instance.
31
     *
32
     * @var \Illuminate\Routing\Router
33
     */
34
    protected $router;
35
36
    /**
37
     * The table headers for the command.
38
     *
39
     * @var array
40
     */
41
    protected $headers = ['Domain', 'Method', 'URI', 'Name', 'Action', 'Middleware'];
42
43
    /**
44
     * The columns to display when using the "compact" flag.
45
     *
46
     * @var array
47
     */
48
    protected $compactColumns = ['method', 'uri', 'action'];
49
50
    /**
51
     * Create a new route command instance.
52
     *
53
     * @param \Illuminate\Routing\Router $router
54
     * @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...
55
     */
56
    public function __construct(Router $router)
57
    {
58
        parent::__construct();
59
60
        $this->router = $router;
61
    }
62
63
    /**
64
     * Execute the console command.
65
     *
66
     * @return void
67
     */
68
    public function handle()
69
    {
70
        $parts = collect($this->router->getRoutes())->map(function ($route) {
71
            return [$this->getPartialPartsMiddleware($route)];
72
        })->filter()->unique();
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