Passed
Pull Request — master (#1293)
by Diego
07:15
created

AdminLteStatusCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
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\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 22
    public function __construct()
65
    {
66 22
        parent::__construct();
67
68
        // Fill the array with the package resources.
69
70 22
        $this->pkgResources = [
71 22
            'assets' => new AdminlteAssetsResource(),
72 22
            'config' => new ConfigResource(),
73 22
            'translations' => new TranslationsResource(),
74 22
            'main_views' => new LayoutViewsResource(),
75 22
            'auth_views' => new AuthViewsResource(),
76 22
            'auth_routes' => new AuthRoutesResource(),
77 22
        ];
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
        $resStatus = $this->getResourcesStatus();
91
        $this->line('');
92
        $this->line('All resources verified successfully!');
93 1
94 1
        // Display the resources installation status.
95
96
        $this->line('');
97
        $this->line('Resources Status:');
98
        $this->showResourcesStatus($resStatus);
99
100
        // Display the status legends table.
101
102 1
        $this->line('');
103
        $this->line('Status Legends:');
104
        $this->showStatusLegends();
105
    }
106 1
107 1
    /**
108 1
     * Gets the resources installation status array.
109 1
     *
110 1
     * @return array
111 1
     */
112
    protected function getResourcesStatus()
113
    {
114
        // Define the array that will hold the resources status.
115 1
116
        $resStatus = [];
117
118
        // Create and initialize a progress bar.
119 1
120 1
        $bar = $this->output->createProgressBar(count($this->pkgResources));
121
        $bar->start();
122
123
        // Get the installation status of each resource.
124
125
        foreach ($this->pkgResources as $name => $resource) {
126
            $resStatus[$name] = $this->getResourceStatus($resource);
127
            $bar->advance();
128 1
        }
129
130
        $bar->finish();
131
132 1
        // Return the resources status.
133
134
        return $resStatus;
135
    }
136 1
137 1
    /**
138
     * Displays the status of the resources.
139
     *
140
     * @param  array  $resStatus  Array with the status of each resource
141 1
     * @return void
142 1
     */
143
    protected function showResourcesStatus($resStatus)
144 1
    {
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
        ];
154
155
        // Create the table rows.
156 1
157
        $tblContent = [];
158
        $notRequiredColor = intval(app()->version()) >= 8 ? 'gray' : 'default';
0 ignored issues
show
introduced by
The method version() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
        $notRequiredColor = intval(app()->/** @scrutinizer ignore-call */ version()) >= 8 ? 'gray' : 'default';
Loading history...
159
160
        foreach ($this->pkgResources as $name => $resource) {
161 1
            $requiredLabel = $resource->required
162 1
                ? 'yes'
163 1
                : $this->styleOutput('no', $notRequiredColor);
164
165
            $tblContent[] = [
166
                $name,
167 1
                $resource->description,
168
                str_replace(base_path('/'), '', $resource->target),
169
                $requiredLabel,
170
                $resStatus[$name] ?? 'Unknown',
171
            ];
172
        }
173
174
        // Display the table.
175
176 1
        $this->table($tblHeader, $tblContent);
177
    }
178 1
179
    /**
180 1
     * Gets the installation status of the specified package resource.
181 1
     *
182 1
     * @param  PackageResource  $resource  The package resource to check
183 1
     * @return string
184
     */
185
    protected function getResourceStatus($resource)
186 1
    {
187
        $status = $this->status['uninstalled'];
188
189
        if ($resource->installed()) {
190
            $status = $this->status['installed'];
191
        } elseif ($resource->exists()) {
192
            $status = $this->status['mismatch'];
193
        }
194 1
195
        return $this->styleOutput($status['label'], $status['color']);
196 1
    }
197
198
    /**
199
     * Displays the legends for the possible status values.
200 1
     *
201 1
     * @return void
202 1
     */
203 1
    protected function showStatusLegends()
204
    {
205
        // Create the table headers for the legends.
206
207 1
        $tblHeader = [
208
            $this->styleOutput('Status', 'cyan'),
209 1
            $this->styleOutput('Description', 'cyan'),
210 1
        ];
211 1
212 1
        // Create the table rows for the legends.
213 1
214
        $tblContent = [];
215
216
        foreach ($this->status as $status) {
217
            $tblContent[] = [
218 1
                $this->styleOutput($status['label'], $status['color']),
219
                $status['legend'],
220
            ];
221
        }
222
223
        // Display the legends table.
224
225
        $this->table($tblHeader, $tblContent);
226
    }
227
228 1
    /**
229
     * Gives output style to the provided text.
230 1
     *
231
     * @param  string  $text  The text to be styled
232
     * @param  string  $color  The output color for the text
233
     * @return string
234
     */
235
    protected function styleOutput($text, $color)
236
    {
237
        return "<fg={$color}>{$text}</>";
238
    }
239
}
240