Passed
Push — master ( ef0dd2...81a39e )
by Florian
05:14
created

AdminLteStatusCommand::showStatusLegends()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

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