Passed
Push — master ( 588cba...7cce6c )
by Florian
03:54
created

AdminLtePluginCommand   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 457
Duplicated Lines 0 %

Test Coverage

Coverage 97.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 138
c 2
b 0
f 0
dl 0
loc 457
ccs 132
cts 135
cp 0.9778
rs 9.76
wmc 33

13 Methods

Rating   Name   Duplication   Size   Complexity  
A removePlugins() 0 42 3
A removePlugin() 0 27 4
A showSummaryTable() 0 8 1
A handle() 0 18 2
A styleOutput() 0 3 1
A showStatusLegends() 0 25 2
A installPlugins() 0 42 3
A getAffectedPlugins() 0 11 2
A __construct() 0 15 1
A getPluginStatus() 0 11 3
B installPlugin() 0 39 7
A showPlugins() 0 11 1
A showPluginsStatus() 0 52 3
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console;
4
5
use Illuminate\Console\Command;
6
use JeroenNoten\LaravelAdminLte\Console\PackageResources\PluginsResource;
7
8
class AdminLtePluginCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'adminlte:plugins
16
        {operation=list : The type of operation: list (default), install or remove}
17
        {--plugin=* : To apply the operation only over the specified plugins, the value should be a plugin key}
18
        {--force : To force the overwrite of existing files}
19
        {--interactive : The installation will guide you through the process}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Manages the installation and removal of additional AdminLTE plugins';
27
28
    /**
29
     * Array with the operations handlers.
30
     *
31
     * @var array
32
     */
33
    protected $opHandlers;
34
35
    /**
36
     * The plugins package resource instance.
37
     *
38
     * @var PluginsResource
39
     */
40
    protected $plugins;
41
42
    /**
43
     * Array with the possible statuses of the plugins.
44
     *
45
     * @var array
46
     */
47
    protected $status = [
48
        'installed' => [
49
            'label' => 'Installed',
50
            'legend' => 'The plugin is installed and matches with the default package plugin',
51
            'color' => 'green',
52
        ],
53
        'mismatch' => [
54
            'label' => 'Mismatch',
55
            'legend' => 'The installed plugin mismatch the package plugin (update available or plugin modified)',
56
            'color' => 'yellow',
57
        ],
58
        'uninstalled' => [
59
            'label' => 'Not Installed',
60
            'legend' => 'The plugin is not installed',
61
            'color' => 'red',
62
        ],
63
    ];
64
65
    /**
66
     * Create a new command instance.
67
     *
68
     * @return void
69
     */
70 17
    public function __construct()
71
    {
72 17
        parent::__construct();
73
74
        // Fill the available operations handlers.
75
76 17
        $this->opHandlers = [
77 17
            'list'    => [$this, 'showPlugins'],
78 17
            'install' => [$this, 'installPlugins'],
79 17
            'remove'  => [$this, 'removePlugins'],
80
        ];
81
82
        // Create the plugins resource instance.
83
84 17
        $this->plugins = new PluginsResource();
85 17
    }
86
87
    /**
88
     * Execute the console command.
89
     *
90
     * @return void
91
     */
92 5
    public function handle()
93
    {
94
        // Get the type of operation to perform.
95
96 5
        $op = $this->argument('operation');
97
98
        // Check if the operation is valid.
99
100 5
        if (! isset($this->opHandlers[$op])) {
101 1
            $this->error("The specified operation: {$op} is not valid!");
102
103 1
            return;
104
        }
105
106
        // Call the handler of the operation.
107
108 4
        $handler = $this->opHandlers[$op];
109 4
        $handler();
110 4
    }
111
112
    /**
113
     * Display a list with the installation status of the plugins.
114
     *
115
     * @return void
116
     */
117 2
    protected function showPlugins()
118
    {
119
        // Show the plugins status.
120
121 2
        $pluginsKeys = $this->getAffectedPlugins();
122 2
        $this->showPluginsStatus($pluginsKeys);
123
124
        // Display the legends table.
125
126 2
        $this->line('');
127 2
        $this->showStatusLegends();
128 2
    }
129
130
    /**
131
     * Get the list of plugins keys that should be affected by an operation.
132
     *
133
     * @return array An array with plugins keys
134
     */
135 4
    protected function getAffectedPlugins()
136
    {
137
        // First, check if the user has specified the plugins keys.
138
139 4
        if (! empty($this->option('plugin'))) {
140 2
            return $this->option('plugin');
141
        }
142
143
        // Otherwise, return a list with all the available plugins keys.
144
145 2
        return array_keys($this->plugins->getSourceData());
146
    }
147
148
    /**
149
     * Display the plugins status.
150
     *
151
     * @param array $pluginsKeys Array with the plugins keys to evaluate
152
     * @return void
153
     */
154 2
    protected function showPluginsStatus($pluginsKeys)
155
    {
156
        // Define the table headers.
157
158
        $tblHeader = [
159 2
            $this->styleOutput('Plugin Name', 'cyan'),
160 2
            $this->styleOutput('Plugin Key', 'cyan'),
161 2
            $this->styleOutput('Status', 'cyan'),
162
        ];
163
164
        // Create a progress bar.
165
166 2
        $bar = $this->output->createProgressBar(count($pluginsKeys));
167
168
        // Initialize the status check procedure.
169
170 2
        $tblContent = [];
171 2
        $this->line('Checking the plugins installation ...');
172 2
        $bar->start();
173
174 2
        foreach ($pluginsKeys as $key) {
175
176
            // Advance the progress bar one step.
177
178 2
            $bar->advance();
179
180
            // Get the plugin data.
181
182 2
            $pluginData = $this->plugins->getSourceData($key);
183
184 2
            if (empty($pluginData)) {
185 1
                $this->line('');
186 1
                $this->error("The plugin key: {$key} is not valid!");
187 1
                continue;
188
            }
189
190
            // Fill the status row of the current plugin.
191
192 2
            $status = $this->getPluginStatus($key);
193 2
            $tblContent[] = [$pluginData['name'], $key, $status];
194
        }
195
196
        // Finish the progress bar.
197
198 2
        $bar->finish();
199 2
        $this->line('');
200 2
        $this->line('All plugins checked succesfully!');
201 2
        $this->line('');
202
203
        // Display the plugins installation status.
204
205 2
        $this->table($tblHeader, $tblContent);
206 2
    }
207
208
    /**
209
     * Get the installation status of a plugin.
210
     *
211
     * @param string $pluginKey The plugin key
212
     * @return string The plugin status
213
     */
214 2
    protected function getPluginStatus($pluginKey)
215
    {
216 2
        $status = $this->status['uninstalled'];
217
218 2
        if ($this->plugins->installed($pluginKey)) {
219 2
            $status = $this->status['installed'];
220 1
        } elseif ($this->plugins->exists($pluginKey)) {
221 1
            $status = $this->status['mismatch'];
222
        }
223
224 2
        return $this->styleOutput($status['label'], $status['color']);
225
    }
226
227
    /**
228
     * Display the legends of the possible status values.
229
     *
230
     * @return void
231
     */
232 2
    protected function showStatusLegends()
233
    {
234 2
        $this->line('Status legends:');
235
236
        // Create the table headers for the legends.
237
238
        $tblHeader = [
239 2
            $this->styleOutput('Status', 'cyan'),
240 2
            $this->styleOutput('Description', 'cyan'),
241
        ];
242
243
        // Create the table rows for the legends.
244
245 2
        $tblContent = [];
246
247 2
        foreach ($this->status as $status) {
248 2
            $tblContent[] = [
249 2
                $this->styleOutput($status['label'], $status['color']),
250 2
                $status['legend'],
251
            ];
252
        }
253
254
        // Display the legends table.
255
256 2
        $this->table($tblHeader, $tblContent);
257 2
    }
258
259
    /**
260
     * Give output style to some text.
261
     *
262
     * @param string $text The text to be styled
263
     * @param string $color The output color for the text
264
     * @return string The styled text
265
     */
266 4
    protected function styleOutput($text, $color)
267
    {
268 4
        return "<fg={$color}>{$text}</>";
269
    }
270
271
    /**
272
     * Installs the specified list of plugins (all if none specified).
273
     *
274
     * @return void
275
     */
276 2
    protected function installPlugins()
277
    {
278 2
        $summary = [];
279
280
        // Get the list of plugins to be installed.
281
282 2
        $pluginsKeys = $this->getAffectedPlugins();
283
284
        // Create a progress bar.
285
286 2
        $bar = $this->output->createProgressBar(count($pluginsKeys));
287 2
        $bar->start();
288
289
        // Install the plugins.
290
291 2
        foreach ($pluginsKeys as $pluginKey) {
292
293
            // Advance the progress bar one step.
294
295 2
            $bar->advance();
296
297
            // Install the plugin.
298
299 2
            if ($this->installPlugin($pluginKey)) {
300 2
                $status = $this->styleOutput('Installed', 'green');
301
            } else {
302 1
                $status = $this->styleOutput('Not Installed / Invalid', 'red');
303
            }
304
305 2
            $summary[] = [$pluginKey, $status];
306
        }
307
308
        // Finish the progress bar.
309
310 2
        $bar->finish();
311 2
        $this->line('');
312 2
        $this->line('The plugins installation is complete. Summary:');
313 2
        $this->line('');
314
315
        // Show summary of installed plugins.
316
317 2
        $this->showSummaryTable($summary);
318 2
    }
319
320
    /**
321
     * Install the specified plugin.
322
     *
323
     * @param string $pluginKey The plugin string key
324
     * @return bool Whether the plugin was succesfully installed
325
     */
326 2
    protected function installPlugin($pluginKey)
327
    {
328
        // Customize the output messages.
329
330 2
        $confirmMsg = $this->plugins->getInstallMessage('install');
331 2
        $overwriteMsg = $this->plugins->getInstallMessage('overwrite');
332
333 2
        $confirmMsg = strtr($confirmMsg, [':plugin' => $pluginKey]);
334 2
        $overwriteMsg = strtr($overwriteMsg, [':plugin' => $pluginKey]);
335
336
        // Check if the plugin is valid.
337
338 2
        if (empty($this->plugins->getSourceData($pluginKey))) {
339 1
            $this->line('');
340 1
            $this->error("The plugin key: {$pluginKey} is not valid!");
341
342 1
            return false;
343
        }
344
345
        // Check if the --interactive option is enabled.
346
347 2
        if ($this->option('interactive') && ! $this->confirm($confirmMsg)) {
348
            return false;
349
        }
350
351
        // Check for overwrite warning.
352
353 2
        $force = $this->option('force');
354 2
        $isOverwrite = ! $force && $this->plugins->exists($pluginKey);
355
356 2
        if ($isOverwrite && ! $this->confirm($overwriteMsg)) {
357
            return false;
358
        }
359
360
        // Install the plugin.
361
362 2
        $this->plugins->install($pluginKey);
363
364 2
        return true;
365
    }
366
367
    /**
368
     * Removes the specified list of plugins (all if none specified).
369
     *
370
     * @return void
371
     */
372 2
    protected function removePlugins()
373
    {
374 2
        $summary = [];
375
376
        // Get the list of plugins to remove.
377
378 2
        $pluginsKeys = $this->getAffectedPlugins();
379
380
        // Create a progress bar.
381
382 2
        $bar = $this->output->createProgressBar(count($pluginsKeys));
383 2
        $bar->start();
384
385
        // Remove the plugins.
386
387 2
        foreach ($pluginsKeys as $pluginKey) {
388
389
            // Advance the progress bar one step.
390
391 2
            $bar->advance();
392
393
            // Remove the plugin.
394
395 2
            if ($this->removePlugin($pluginKey)) {
396 2
                $status = $this->styleOutput('Removed', 'green');
397
            } else {
398 1
                $status = $this->styleOutput('Not Removed / Invalid', 'red');
399
            }
400
401 2
            $summary[] = [$pluginKey, $status];
402
        }
403
404
        // Finish the progress bar.
405
406 2
        $bar->finish();
407 2
        $this->line('');
408 2
        $this->line('The plugins removal is complete. Summary:');
409 2
        $this->line('');
410
411
        // Show summary of removed plugins.
412
413 2
        $this->showSummaryTable($summary);
414 2
    }
415
416
    /**
417
     * Remove/Uninstall the specified plugin.
418
     *
419
     * @param string $pluginKey The plugin string key
420
     * @return bool Whether the plugin was succesfully removed
421
     */
422 2
    protected function removePlugin($pluginKey)
423
    {
424
        // Customize the output messages.
425
426 2
        $confirmMsg = $this->plugins->getInstallMessage('remove');
427 2
        $confirmMsg = strtr($confirmMsg, [':plugin' => $pluginKey]);
428
429
        // Check if the plugin is valid.
430
431 2
        if (empty($this->plugins->getSourceData($pluginKey))) {
432 1
            $this->line('');
433 1
            $this->error("The plugin key: {$pluginKey} is not valid!");
434
435 1
            return false;
436
        }
437
438
        // Check if the --interactive option is enabled.
439
440 2
        if ($this->option('interactive') && ! $this->confirm($confirmMsg)) {
441
            return false;
442
        }
443
444
        // Remove the plugin.
445
446 2
        $this->plugins->uninstall($pluginKey);
447
448 2
        return true;
449
    }
450
451
    /**
452
     * Show the summary table for some operation.
453
     *
454
     * @param array $rows The table rows.
455
     * @return void
456
     */
457 2
    protected function showSummaryTable($rows)
458
    {
459
        $header = [
460 2
            $this->styleOutput('Plugin Key', 'cyan'),
461 2
            $this->styleOutput('Status', 'cyan'),
462
        ];
463
464 2
        $this->table($header, $rows);
465 2
    }
466
}
467