AdminLteStatusCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 3 Features 0
Metric Value
eloc 81
dl 0
loc 229
ccs 76
cts 76
cp 1
rs 10
c 3
b 3
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A handle() 0 20 1
A showStatusLegends() 0 23 2
A styleOutput() 0 3 1
A getResourcesStatus() 0 23 2
A getResourceStatus() 0 11 3
A showResourcesStatus() 0 37 4
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console;
4
5
use Illuminate\Console\Command;
6
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AdminlteAssetsResource;
7
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthRoutesResource;
8
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthViewsResource;
9
use JeroenNoten\LaravelAdminLte\Console\PackageResources\BladeComponentsResource;
10
use JeroenNoten\LaravelAdminLte\Console\PackageResources\ConfigResource;
11
use JeroenNoten\LaravelAdminLte\Console\PackageResources\LayoutViewsResource;
12
use JeroenNoten\LaravelAdminLte\Console\PackageResources\TranslationsResource;
13
14
class AdminLteStatusCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'adminlte:status ';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Checks the installation status of the package resources';
29
30
    /**
31
     * Array with all the available package resources.
32
     *
33
     * @var array
34
     */
35
    protected $pkgResources;
36
37
    /**
38
     * Array with the possible resources statuses.
39
     *
40
     * @var array
41
     */
42
    protected $status = [
43
        'installed' => [
44
            'label' => 'Installed',
45
            'legend' => 'The resource is published and matches with the package original resource',
46
            'color' => 'green',
47
        ],
48
        'mismatch' => [
49
            'label' => 'Mismatch',
50
            'legend' => 'The resource is published but mismatches with the package original resource (update available or resource modified)',
51
            'color' => 'yellow',
52
        ],
53
        'uninstalled' => [
54
            'label' => 'Not Installed',
55
            'legend' => 'The resource is not published',
56
            'color' => 'red',
57
        ],
58
    ];
59
60
    /**
61
     * Create a new command instance.
62
     *
63
     * @return void
64
     */
65 28
    public function __construct()
66
    {
67 28
        parent::__construct();
68
69
        // Fill the array with the package resources.
70
71 28
        $this->pkgResources = [
72 28
            'assets' => new AdminlteAssetsResource(),
73 28
            'config' => new ConfigResource(),
74 28
            'translations' => new TranslationsResource(),
75 28
            'main_views' => new LayoutViewsResource(),
76 28
            'auth_views' => new AuthViewsResource(),
77 28
            'auth_routes' => new AuthRoutesResource(),
78 28
            'components' => new BladeComponentsResource(),
79 28
        ];
80
    }
81
82
    /**
83
     * Execute the console command.
84
     *
85
     * @return void
86
     */
87 1
    public function handle()
88
    {
89
        // Get the installation status of each resource.
90
91 1
        $this->line('Verifying the installation of the resources...');
92 1
        $resStatus = $this->getResourcesStatus();
93 1
        $this->line('');
94 1
        $this->line('All resources verified successfully!');
95
96
        // Display the resources installation status.
97
98 1
        $this->line('');
99 1
        $this->line('Resources Status:');
100 1
        $this->showResourcesStatus($resStatus);
101
102
        // Display the status legends table.
103
104 1
        $this->line('');
105 1
        $this->line('Status Legends:');
106 1
        $this->showStatusLegends();
107
    }
108
109
    /**
110
     * Gets the resources installation status array.
111
     *
112
     * @return array
113
     */
114 1
    protected function getResourcesStatus()
115
    {
116
        // Define the array that will hold the resources status.
117
118 1
        $resStatus = [];
119
120
        // Create and initialize a progress bar.
121
122 1
        $bar = $this->output->createProgressBar(count($this->pkgResources));
123 1
        $bar->start();
124
125
        // Get the installation status of each resource.
126
127 1
        foreach ($this->pkgResources as $name => $resource) {
128 1
            $resStatus[$name] = $this->getResourceStatus($resource);
129 1
            $bar->advance();
130
        }
131
132 1
        $bar->finish();
133
134
        // Return the resources status.
135
136 1
        return $resStatus;
137
    }
138
139
    /**
140
     * Displays the status of the resources.
141
     *
142
     * @param  array  $resStatus  Array with the status of each resource
143
     * @return void
144
     */
145 1
    protected function showResourcesStatus($resStatus)
146
    {
147
        // Define the table headers.
148
149 1
        $tblHeader = [
150 1
            $this->styleOutput('Package Resource', 'cyan'),
151 1
            $this->styleOutput('Description', 'cyan'),
152 1
            $this->styleOutput('Publishing Target', 'cyan'),
153 1
            $this->styleOutput('Required', 'cyan'),
154 1
            $this->styleOutput('Status', 'cyan'),
155 1
        ];
156
157
        // Create the table rows.
158
159 1
        $tblContent = [];
160
161 1
        foreach ($this->pkgResources as $name => $resource) {
162 1
            $requiredLabel = $resource->required
163 1
                ? $this->styleOutput('yes', 'green')
164 1
                : 'no';
165
166 1
            $publishingTarget = is_array($resource->target)
167 1
                ? implode(PHP_EOL, $resource->target)
168 1
                : $resource->target;
169
170 1
            $tblContent[] = [
171 1
                $name,
172 1
                $resource->description,
173 1
                str_replace(base_path().'/', '', $publishingTarget),
174 1
                $requiredLabel,
175 1
                $resStatus[$name] ?? 'Unknown',
176 1
            ];
177
        }
178
179
        // Display the table.
180
181 1
        $this->table($tblHeader, $tblContent);
182
    }
183
184
    /**
185
     * Gets the installation status of the specified package resource.
186
     *
187
     * @param  PackageResource  $resource  The package resource to check
188
     * @return string
189
     */
190 1
    protected function getResourceStatus($resource)
191
    {
192 1
        $status = $this->status['uninstalled'];
193
194 1
        if ($resource->installed()) {
195 1
            $status = $this->status['installed'];
196 1
        } elseif ($resource->exists()) {
197 1
            $status = $this->status['mismatch'];
198
        }
199
200 1
        return $this->styleOutput($status['label'], $status['color']);
201
    }
202
203
    /**
204
     * Displays the legends for the possible status values.
205
     *
206
     * @return void
207
     */
208 1
    protected function showStatusLegends()
209
    {
210
        // Create the table headers for the legends.
211
212 1
        $tblHeader = [
213 1
            $this->styleOutput('Status', 'cyan'),
214 1
            $this->styleOutput('Description', 'cyan'),
215 1
        ];
216
217
        // Create the table rows for the legends.
218
219 1
        $tblContent = [];
220
221 1
        foreach ($this->status as $status) {
222 1
            $tblContent[] = [
223 1
                $this->styleOutput($status['label'], $status['color']),
224 1
                $status['legend'],
225 1
            ];
226
        }
227
228
        // Display the legends table.
229
230 1
        $this->table($tblHeader, $tblContent);
231
    }
232
233
    /**
234
     * Gives output style to the provided text.
235
     *
236
     * @param  string  $text  The text to be styled
237
     * @param  string  $color  The output color for the text
238
     * @return string
239
     */
240 1
    protected function styleOutput($text, $color)
241
    {
242 1
        return "<fg={$color}>{$text}</>";
243
    }
244
}
245