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

AdminLteInstallCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 29
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 47
ccs 19
cts 19
cp 1
crap 1
rs 9.456
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console;
4
5
use Illuminate\Console\Command;
6
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AssetsResource;
7
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthViewsResource;
8
use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicRoutesResource;
9
use JeroenNoten\LaravelAdminLte\Console\PackageResources\BasicViewsResource;
10
use JeroenNoten\LaravelAdminLte\Console\PackageResources\ConfigResource;
11
use JeroenNoten\LaravelAdminLte\Console\PackageResources\MainViewsResource;
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 (default), enhanced or full}
23
        {--only=* : To install only specific resources: assets, config, translations, auth_views, basic_views, basic_routes or main_views. Can\'t be used with option --with}
24
        {--with=* : To install with additional resources: auth_views, basic_views, basic_routes or main_views}
25
        {--force : To force the overwrite of existing files}
26
        {--interactive : The installation will guide you through the process}';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Install all the required files for AdminLTE, and additional resources';
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 17
    public function __construct()
76
    {
77 17
        parent::__construct();
78
79
        // Fill the array with the package resources.
80
81 17
        $this->pkgResources = [
82 17
            'assets'       => new AssetsResource(),
83 17
            'config'       => new ConfigResource(),
84 17
            'translations' => new TranslationsResource(),
85 17
            'main_views'   => new MainViewsResource(),
86 17
            'auth_views'   => new AuthViewsResource(),
87 17
            'basic_views'  => new BasicViewsResource(),
88 17
            'basic_routes' => new BasicRoutesResource(),
89
        ];
90
91
        // Add the resources related to each available --type option.
92
93 17
        $basic = ['assets', 'config', 'translations'];
94 17
        $enhanced = array_merge($basic, ['auth_views']);
95 17
        $full = array_merge($enhanced, ['basic_views', 'basic_routes']);
96
97 17
        $this->optTypeResources = [
98 17
            'basic'    => $basic,
99 17
            'enhanced' => $enhanced,
100 17
            'full'     => $full,
101
        ];
102
103
        // Add the resources related to each available --only option.
104
105 17
        $this->optOnlyResources = [
106
            'assets'       => ['assets'],
107
            'config'       => ['config'],
108
            'translations' => ['translations'],
109
            'main_views'   => ['main_views'],
110
            'auth_views'   => ['auth_views'],
111
            'basic_views'  => ['basic_views'],
112
            'basic_routes' => ['basic_routes'],
113
        ];
114
115
        // Add the resources related to each available --with option.
116
117 17
        $this->optWithResources = [
118
            'main_views'   => ['main_views'],
119
            'auth_views'   => ['auth_views'],
120
            'basic_views'  => ['basic_views'],
121
            'basic_routes' => ['basic_routes'],
122
        ];
123 17
    }
124
125
    /**
126
     * Execute the console command.
127
     *
128
     * @return void
129
     */
130 11
    public function handle()
131
    {
132
        // Reset the variable that keep track of the installed packages.
133
134 11
        $this->installedResources = [];
135
136
        // Check if option --only is used. In this case, install the specified
137
        // parts and return.
138
139 11
        if ($optValues = $this->option('only')) {
140 6
            $this->handleOptions($optValues, $this->optOnlyResources, 'only');
141
142 6
            return;
143
        }
144
145
        // Handle the --type option. Default value for this option is "basic"
146
        // installation type.
147
148 5
        $optValue = $this->option('type');
149 5
        $this->handleOption($optValue, $this->optTypeResources, 'type');
150
151
        // Check if option --with is used. In this case, also install the
152
        // specified parts.
153
154 5
        if ($optValues = $this->option('with')) {
155 2
            $this->handleOptions($optValues, $this->optWithResources, 'with');
156
        }
157
158 5
        $this->line('The installation is complete.');
159 5
    }
160
161
    /**
162
     * Handle multiple option values.
163
     *
164
     * @param array $values An array with the option values
165
     * @param array $resources An array with the resources of each option
166
     * @param string $opt Descriptive name of the handled option
167
     * @return void
168
     */
169 8
    protected function handleOptions($values, $resources, $opt)
170
    {
171 8
        foreach ($values as $value) {
172 8
            $this->handleOption($value, $resources, $opt);
173
        }
174 8
    }
175
176
    /**
177
     * Handle an option value.
178
     *
179
     * @param string $value A string with the option value
180
     * @param array $resources An array with the resources of each option
181
     * @param string $opt Descriptive name of the handled option
182
     * @return void
183
     */
184 11
    protected function handleOption($value, $resources, $opt)
185
    {
186 11
        if (! isset($resources[$value])) {
187 1
            $this->comment("The option --{$opt}={$value} is invalid!");
188
189 1
            return;
190
        }
191
192
        // Install all the resources related to the option value.
193
194 10
        $this->exportPackageResources(...$resources[$value]);
195 10
    }
196
197
    /**
198
     * Install multiple packages resources.
199
     *
200
     * @param string $resources The resources to install
201
     * @return void
202
     */
203 10
    protected function exportPackageResources(...$resources)
204
    {
205 10
        foreach ($resources as $resource) {
206
207
            // Check if resource was already installed on the current command
208
            // execution. This can happen, for example, when using:
209
            // php artisan --type=full --with=auth_views
210
211 10
            if (isset($this->installedResources[$resource])) {
212 1
                continue;
213
            }
214
215 10
            $this->exportPackageResource($resource);
216 10
            $this->installedResources[$resource] = true;
217
        }
218 10
    }
219
220
    /**
221
     * Install a package resource.
222
     *
223
     * @param string $resource The keyword of the resource to install
224
     * @return void
225
     */
226 10
    protected function exportPackageResource($resource)
227
    {
228 10
        $resource = $this->pkgResources[$resource];
229 10
        $installMsg = $resource->getInstallMessage('install');
230 10
        $overwriteMsg = $resource->getInstallMessage('overwrite');
231 10
        $successMsg = $resource->getInstallMessage('success');
232
233
        // Check if the --interactive option is enabled.
234
235 10
        if ($this->option('interactive') && ! $this->confirm($installMsg)) {
236
            return;
237
        }
238
239
        // Check for overwrite warning.
240
241 10
        $isOverwrite = ! $this->option('force') && $resource->exists();
242
243 10
        if ($isOverwrite && ! $this->confirm($overwriteMsg)) {
244
            return;
245
        }
246
247
        // Install the resource.
248
249 10
        $resource->install();
250 10
        $this->info($successMsg);
251 10
    }
252
}
253