Passed
Pull Request — master (#1299)
by Diego
05:29 queued 02:01
created

AdminLteRemoveCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 1
b 0
f 0
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 27
    public function __construct()
46
    {
47 27
        parent::__construct();
48
49
        // Fill the array with the available package resources.
50
51 27
        $this->pkgResources = [
52 27
            'assets' => new AdminlteAssetsResource(),
53 27
            'config' => new ConfigResource(),
54 27
            'translations' => new TranslationsResource(),
55 27
            'main_views' => new LayoutViewsResource(),
56 27
            'auth_views' => new AuthViewsResource(),
57 27
            'auth_routes' => new AuthRoutesResource(),
58 27
            'components' => new BladeComponentsResource(),
59 27
        ];
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return void
66
     */
67 4
    public function handle()
68
    {
69
        // Read and verify the resources to be uninstalled.
70
71 4
        foreach ($this->argument('resource') as $res) {
72
            // Check if resource is valid.
73
74 4
            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 3
            $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 3
    protected function uninstallPackageResource($resource)
93
    {
94 3
        $removeMsg = 'Do you really want to uninstall the resource: :res?';
95 3
        $removeMsg = str_replace(':res', $resource, $removeMsg);
96 3
        $resource = $this->pkgResources[$resource];
97
98
        // Check if the --interactive option is enabled.
99
100 3
        if ($this->option('interactive') && ! $this->confirm($removeMsg)) {
101 1
            return;
102
        }
103
104
        // Check whether to warn for uninstalling a required resource.
105
106 2
        $requiredWarnMsg = 'This resource is required by the package, ';
107 2
        $requiredWarnMsg .= 'do you really want to uninstall it?';
108
109 2
        $shouldWarn = ! $this->option('force') && $resource->required;
110
111 2
        if ($shouldWarn && ! $this->confirm($requiredWarnMsg)) {
112 1
            return;
113
        }
114
115
        // Uninstall the resource.
116
117 1
        $resource->uninstall();
118 1
        $this->info('The resource was successfully removed');
119
    }
120
}
121