AdminLteRemoveCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A uninstallPackageResource() 0 27 6
A handle() 0 16 3
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 28
    public function __construct()
46
    {
47 28
        parent::__construct();
48
49
        // Fill the array with the available package resources.
50
51 28
        $this->pkgResources = [
52 28
            'assets' => new AdminlteAssetsResource(),
53 28
            'config' => new ConfigResource(),
54 28
            'translations' => new TranslationsResource(),
55 28
            'main_views' => new LayoutViewsResource(),
56 28
            'auth_views' => new AuthViewsResource(),
57 28
            'auth_routes' => new AuthRoutesResource(),
58 28
            'components' => new BladeComponentsResource(),
59 28
        ];
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return void
66
     */
67 5
    public function handle()
68
    {
69
        // Read and verify the resources to be uninstalled.
70
71 5
        foreach ($this->argument('resource') as $res) {
72
            // Check if resource is valid.
73
74 5
            if (! isset($this->pkgResources[$res])) {
75 1
                $this->comment("The provided resource: {$res} is invalid!");
76
77 1
                continue;
78
            }
79
80
            // Uninstall the resource.
81
82 4
            $this->uninstallPackageResource($res);
83
        }
84
    }
85
86
    /**
87
     * Uninstalls a package resource.
88
     *
89
     * @param  string  $resource  The keyword of the resource to uninstall
90
     * @return void
91
     */
92 4
    protected function uninstallPackageResource($resource)
93
    {
94 4
        $removeMsg = 'Do you really want to uninstall the resource: :res?';
95 4
        $removeMsg = str_replace(':res', $resource, $removeMsg);
96 4
        $resource = $this->pkgResources[$resource];
97
98
        // Check if the --interactive option is enabled.
99
100 4
        if ($this->option('interactive') && ! $this->confirm($removeMsg)) {
101 1
            return;
102
        }
103
104
        // Check whether to warn for uninstalling a required resource.
105
106 3
        $requiredWarnMsg = 'This resource is required by the package, ';
107 3
        $requiredWarnMsg .= 'do you really want to uninstall it?';
108
109 3
        $shouldWarn = ! $this->option('force') && $resource->required;
110
111 3
        if ($shouldWarn && ! $this->confirm($requiredWarnMsg)) {
112 1
            return;
113
        }
114
115
        // Uninstall the resource.
116
117 2
        $resource->uninstall();
118 2
        $this->info('The resource was successfully removed');
119
    }
120
}
121