Passed
Push — master ( 70acef...8b9af3 )
by Florian
07:10 queued 02:43
created

AdminLteStatusCommand::checkAsset()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 6
eloc 18
c 2
b 2
f 0
nc 5
nop 2
dl 0
loc 26
ccs 0
cts 18
cp 0
crap 42
rs 9.0444
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 AdminLteStatusCommand extends Command
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'adminlte:status ';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Checks the installation status of the AdminLTE resources';
29
30
    /**
31
     * Array with all the available package resources.
32
     *
33
     * @var array
34
     */
35
    protected $pkgResources;
36
37
    /**
38
     * Array with the possible resources statuses.
39
     *
40
     * @var array
41
     */
42
    protected $status = [
43
        'installed' => [
44
            'label' => 'Installed',
45
            'legend' => 'The resource is installed and matches with the default package resource',
46
            'color' => 'green',
47
        ],
48
        'mismatch' => [
49
            'label' => 'Mismatch',
50
            'legend' => 'The installed resource mismatch the package resource (update available or resource modified)',
51
            'color' => 'yellow',
52
        ],
53
        'uninstalled' => [
54
            'label' => 'Not Installed',
55
            'legend' => 'The package resource is not installed',
56
            'color' => 'red',
57
        ],
58
    ];
59
60
    /**
61
     * Create a new command instance.
62
     *
63
     * @return void
64
     */
65 17
    public function __construct()
66
    {
67 17
        parent::__construct();
68
69
        // Fill the array with the package resources.
70
71 17
        $this->pkgResources = [
72 17
            'assets'       => new AssetsResource(),
73 17
            'config'       => new ConfigResource(),
74 17
            'translations' => new TranslationsResource(),
75 17
            'main_views'   => new MainViewsResource(),
76 17
            'auth_views'   => new AuthViewsResource(),
77 17
            'basic_views'  => new BasicViewsResource(),
78 17
            'basic_routes' => new BasicRoutesResource(),
79
        ];
80 17
    }
81
82
    /**
83
     * Execute the console command.
84
     *
85
     * @return void
86
     */
87 1
    public function handle()
88
    {
89
        // Display the resources installation status.
90
91 1
        $this->showResourcesStatus();
92
93
        // Display the legends table.
94
95 1
        $this->line('');
96 1
        $this->showStatusLegends();
97 1
    }
98
99
    /**
100
     * Display the resources status.
101
     *
102
     * @return void
103
     */
104 1
    protected function showResourcesStatus()
105
    {
106
        // Define the table headers.
107
108
        $tblHeader = [
109 1
            $this->styleOutput('Package Resource', 'cyan'),
110 1
            $this->styleOutput('Description', 'cyan'),
111 1
            $this->styleOutput('Status', 'cyan'),
112 1
            $this->styleOutput('Required', 'cyan'),
113
        ];
114
115
        // Get the table rows.
116
117 1
        $tblContent = $this->getResourcesStatusRows();
118
119
        // Display the table.
120
121 1
        $this->line('');
122 1
        $this->table($tblHeader, $tblContent);
123 1
    }
124
125
    /**
126
     * Get the rows for the resources status table.
127
     *
128
     * @return array
129
     */
130 1
    protected function getResourcesStatusRows()
131
    {
132
        // Define the array that will hold the table rows.
133
134 1
        $tblContent = [];
135
136
        // Create a progress bar.
137
138 1
        $steps = count($this->pkgResources);
139 1
        $bar = $this->output->createProgressBar($steps);
140
141
        // Initialize the status check procedure.
142
143 1
        $this->line('Checking the resources installation ...');
144 1
        $bar->start();
145
146 1
        foreach ($this->pkgResources as $name => $resource) {
147
148
            // Fill the status row of the current resource.
149
150 1
            $tblContent[] = [
151 1
                $name,
152 1
                $resource->description,
153 1
                $this->getResourceStatus($resource),
154 1
                var_export($resource->required, true),
155
            ];
156
157
            // Advance the progress bar one step.
158
159 1
            $bar->advance();
160
        }
161
162
        // Finish the progress bar.
163
164 1
        $bar->finish();
165 1
        $this->line('');
166 1
        $this->line('All resources checked succesfully!');
167
168
        // Return the rows.
169
170 1
        return $tblContent;
171
    }
172
173
    /**
174
     * Get the installation status of a package resource.
175
     *
176
     * @param PackageResource $resource The package resource to check
177
     * @return string The resource status
178
     */
179 1
    protected function getResourceStatus($resource)
180
    {
181 1
        $status = $this->status['uninstalled'];
182
183 1
        if ($resource->installed()) {
184 1
            $status = $this->status['installed'];
185 1
        } elseif ($resource->exists()) {
186 1
            $status = $this->status['mismatch'];
187
        }
188
189 1
        return $this->styleOutput($status['label'], $status['color']);
190
    }
191
192
    /**
193
     * Display the legends of the possible status values.
194
     *
195
     * @return void
196
     */
197 1
    protected function showStatusLegends()
198
    {
199 1
        $this->line('Status legends:');
200
201
        // Create the table headers for the legends.
202
203
        $tblHeader = [
204 1
            $this->styleOutput('Status', 'cyan'),
205 1
            $this->styleOutput('Description', 'cyan'),
206
        ];
207
208
        // Create the table rows for the legends.
209
210 1
        $tblContent = [];
211
212 1
        foreach ($this->status as $status) {
213 1
            $tblContent[] = [
214 1
                $this->styleOutput($status['label'], $status['color']),
215 1
                $status['legend'],
216
            ];
217
        }
218
219
        // Display the legends table.
220
221 1
        $this->table($tblHeader, $tblContent);
222 1
    }
223
224
    /**
225
     * Give output style to some text.
226
     *
227
     * @param string $text The text to be styled
228
     * @param string $color The output color for the text
229
     * @return string The styled text
230
     */
231 1
    protected function styleOutput($text, $color)
232
    {
233 1
        return "<fg={$color}>{$text}</>";
234
    }
235
}
236