Passed
Pull Request — master (#1299)
by Diego
04:09
created

AdminLteRemoveCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 36.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 106
ccs 11
cts 30
cp 0.3667
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstallPackageResource() 0 27 6
A handle() 0 17 3
A __construct() 0 14 1
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 AdminLteRemoveCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'adminlte:remove
22
        {resource* : The resource to uninstall: assets, config, translations, auth_views, auth_routes, main_views or components}
23
        {--force : To force the uninstall procedure without warnings alerts}
24
        {--interactive : To allow the uninstall process guide you through it}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Uninstalls one or more specified resources';
32
33
    /**
34
     * Array with all the available package resources.
35
     *
36
     * @var array
37
     */
38
    protected $pkgResources;
39
40
    /**
41
     * Create a new command instance.
42
     *
43
     * @return void
44
     */
45 23
    public function __construct()
46
    {
47 23
        parent::__construct();
48
49
        // Fill the array with the available package resources.
50
51 23
        $this->pkgResources = [
52 23
            'assets' => new AdminlteAssetsResource(),
53 23
            'config' => new ConfigResource(),
54 23
            'translations' => new TranslationsResource(),
55 23
            'main_views' => new LayoutViewsResource(),
56 23
            'auth_views' => new AuthViewsResource(),
57 23
            'auth_routes' => new AuthRoutesResource(),
58 23
            'components' => new BladeComponentsResource(),
59 23
        ];
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return void
66
     */
67
    public function handle()
68
    {
69
        // Read and verify the resources to be uninstalled.
70
71
        foreach ($this->argument('resource') as $res) {
72
73
            // Check if resource is valid.
74
75
            if (! isset($this->pkgResources[$res])) {
76
                $this->comment("The provided resource: {$res} is invalid!");
77
78
                continue;
79
            }
80
81
            // Uninstall the resource.
82
83
            $this->uninstallPackageResource($res);
84
        }
85
    }
86
87
    /**
88
     * Uninstalls a package resource.
89
     *
90
     * @param  string  $resource  The keyword of the resource to uninstall
91
     * @return void
92
     */
93
    protected function uninstallPackageResource($resource)
94
    {
95
        $removeMsg = 'Do you really want to uninstall the resource: :res?';
96
        $removeMsg = str_replace(':res', $resource, $removeMsg);
97
        $resource = $this->pkgResources[$resource];
98
99
        // Check if the --interactive option is enabled.
100
101
        if ($this->option('interactive') && ! $this->confirm($removeMsg)) {
102
            return;
103
        }
104
105
        // Check whether to warn for uninstalling a required resource.
106
107
        $requiredWarnMsg = 'This resource is required by the package, ';
108
        $requiredWarnMsg .= 'do you really want to uninstall it?';
109
110
        $shouldWarn = ! $this->option('force') && $resource->required;
111
112
        if ($shouldWarn && ! $this->confirm($requiredWarnMsg)) {
113
            return;
114
        }
115
116
        // Uninstall the resource.
117
118
        $resource->uninstall();
119
        $this->info('The resource was successfully removed.');
120
    }
121
}
122