AdminLteInstallCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 70
c 6
b 2
f 0
dl 0
loc 238
ccs 72
cts 72
cp 1
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 49 1
A exportPackageResource() 0 25 6
A handleOptions() 0 4 2
A exportPackageResources() 0 13 3
A handle() 0 29 3
A handleOption() 0 11 2
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 AdminLteInstallCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'adminlte:install
22
        {--type=basic : The installation type: basic, basic_with_auth, basic_with_views or full}
23
        {--only=* : To install only specific resources: assets, config, translations, auth_views, auth_routes, main_views or components. Can\'t be mixed with option --with}
24
        {--with=* : To install with additional resources: auth_views, auth_routes, main_views or components}
25
        {--force : To force the overwrite of existing files during the installation process}
26
        {--interactive : To allow the installation process guide you through it}';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Publishes the required files, and other resources, for use the AdminLTE template';
34
35
    /**
36
     * Array with all the available package resources.
37
     *
38
     * @var array
39
     */
40
    protected $pkgResources;
41
42
    /**
43
     * Array with all the installed packages on the current execution.
44
     *
45
     * @var array
46
     */
47
    protected $installedResources;
48
49
    /**
50
     * Array with the resources available for the --only options.
51
     *
52
     * @var array
53
     */
54
    protected $optOnlyResources;
55
56
    /**
57
     * Array with the resources available for the --with options.
58
     *
59
     * @var array
60
     */
61
    protected $optWithResources;
62
63
    /**
64
     * Array with the resources available for the --type options.
65
     *
66
     * @var array
67
     */
68
    protected $optTypeResources;
69
70
    /**
71
     * Create a new command instance.
72
     *
73
     * @return void
74
     */
75 28
    public function __construct()
76
    {
77 28
        parent::__construct();
78
79
        // Fill the array with the available package resources.
80
81 28
        $this->pkgResources = [
82 28
            'assets' => new AdminlteAssetsResource(),
83 28
            'config' => new ConfigResource(),
84 28
            'translations' => new TranslationsResource(),
85 28
            'main_views' => new LayoutViewsResource(),
86 28
            'auth_views' => new AuthViewsResource(),
87 28
            'auth_routes' => new AuthRoutesResource(),
88 28
            'components' => new BladeComponentsResource(),
89 28
        ];
90
91
        // Add the resources related to each available --type option.
92
93 28
        $basic = ['assets', 'config', 'translations'];
94 28
        $basicWithAuth = array_merge($basic, ['auth_views', 'auth_routes']);
95 28
        $basicWithViews = array_merge($basic, ['main_views']);
96 28
        $full = array_merge($basicWithAuth, ['main_views', 'components']);
97
98 28
        $this->optTypeResources = [
99 28
            'basic' => $basic,
100 28
            'basic_with_auth' => $basicWithAuth,
101 28
            'basic_with_views' => $basicWithViews,
102 28
            'full' => $full,
103 28
        ];
104
105
        // Add the resources related to each available --only option.
106
107 28
        $this->optOnlyResources = [
108 28
            'assets' => ['assets'],
109 28
            'config' => ['config'],
110 28
            'translations' => ['translations'],
111 28
            'main_views' => ['main_views'],
112 28
            'auth_views' => ['auth_views'],
113 28
            'auth_routes' => ['auth_routes'],
114 28
            'components' => ['components'],
115 28
        ];
116
117
        // Add the resources related to each available --with option.
118
119 28
        $this->optWithResources = [
120 28
            'main_views' => ['main_views'],
121 28
            'auth_views' => ['auth_views'],
122 28
            'auth_routes' => ['auth_routes'],
123 28
            'components' => ['components'],
124 28
        ];
125
    }
126
127
    /**
128
     * Execute the console command.
129
     *
130
     * @return void
131
     */
132 15
    public function handle()
133
    {
134
        // Reset the variable that keep track of the installed packages.
135
136 15
        $this->installedResources = [];
137
138
        // Check if option --only is used. In this case, install only the
139
        // specified resources and finish.
140
141 15
        if ($optValues = $this->option('only')) {
142 9
            $this->handleOptions($optValues, $this->optOnlyResources, 'only');
143
144 9
            return;
145
        }
146
147
        // Handle the --type option. Default value for this option is "basic"
148
        // installation type.
149
150 6
        $optValue = $this->option('type');
151 6
        $this->handleOption($optValue, $this->optTypeResources, 'type');
152
153
        // Check if option --with is used. In this case, we also need to install
154
        // the specified additional resources.
155
156 6
        if ($optValues = $this->option('with')) {
157 2
            $this->handleOptions($optValues, $this->optWithResources, 'with');
158
        }
159
160 6
        $this->line('The installation is complete.');
161
    }
162
163
    /**
164
     * Handles multiple option values.
165
     *
166
     * @param  array  $values  An array with the option values
167
     * @param  array  $resources  An array with the resources of each option
168
     * @param  string  $opt  Descriptive name of the handled option
169
     * @return void
170
     */
171 11
    protected function handleOptions($values, $resources, $opt)
172
    {
173 11
        foreach ($values as $value) {
174 11
            $this->handleOption($value, $resources, $opt);
175
        }
176
    }
177
178
    /**
179
     * Handles an option value.
180
     *
181
     * @param  string  $value  A string with the option value
182
     * @param  array  $resources  An array with the resources of each option
183
     * @param  string  $opt  Descriptive name of the handled option
184
     * @return void
185
     */
186 15
    protected function handleOption($value, $resources, $opt)
187
    {
188 15
        if (! isset($resources[$value])) {
189 1
            $this->comment("The option --{$opt}={$value} is invalid!");
190
191 1
            return;
192
        }
193
194
        // Install all the resources related to the option value.
195
196 14
        $this->exportPackageResources(...$resources[$value]);
197
    }
198
199
    /**
200
     * Installs multiple packages resources.
201
     *
202
     * @param  string  $resources  The resources to install
203
     * @return void
204
     */
205 14
    protected function exportPackageResources(...$resources)
206
    {
207 14
        foreach ($resources as $resource) {
208
            // Check if the resource was already installed on the current
209
            // command execution. This can happen, for example, when using:
210
            // php artisan --type=full --with=auth_views
211
212 14
            if (isset($this->installedResources[$resource])) {
213 1
                continue;
214
            }
215
216 14
            $this->exportPackageResource($resource);
217 14
            $this->installedResources[$resource] = true;
218
        }
219
    }
220
221
    /**
222
     * Installs a package resource.
223
     *
224
     * @param  string  $resource  The keyword of the resource to install
225
     * @return void
226
     */
227 14
    protected function exportPackageResource($resource)
228
    {
229 14
        $resource = $this->pkgResources[$resource];
230 14
        $installMsg = $resource->getInstallMessage('install');
231 14
        $overwriteMsg = $resource->getInstallMessage('overwrite');
232 14
        $successMsg = $resource->getInstallMessage('success');
233
234
        // Check if the --interactive option is enabled.
235
236 14
        if ($this->option('interactive') && ! $this->confirm($installMsg)) {
237 1
            return;
238
        }
239
240
        // Check for overwrite warning.
241
242 14
        $shouldWarnOverwrite = ! $this->option('force') && $resource->exists();
243
244 14
        if ($shouldWarnOverwrite && ! $this->confirm($overwriteMsg)) {
245 1
            return;
246
        }
247
248
        // Install the resource.
249
250 14
        $resource->install();
251 14
        $this->info($successMsg);
252
    }
253
}
254