Skip to content

Commit 6438f04

Browse files
authored
Refactor app commands (alexjustesen#1458)
1 parent 4abae28 commit 6438f04

File tree

9 files changed

+84
-212
lines changed

9 files changed

+84
-212
lines changed

app/Console/Commands/InstallCommand.php

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace App\Console\Commands;
44

55
use Illuminate\Console\Command;
6-
use Illuminate\Support\Facades\Artisan;
6+
7+
use function Laravel\Prompts\confirm;
8+
use function Laravel\Prompts\info;
79

810
class InstallCommand extends Command
911
{
@@ -19,71 +21,38 @@ class InstallCommand extends Command
1921
*
2022
* @var string
2123
*/
22-
protected $description = 'A fresh install of Speedtest Tracker.';
24+
protected $description = 'Install a fresh version of the Speedtest Tracker application.';
2325

2426
/**
2527
* Execute the console command.
2628
*/
2729
public function handle(): void
2830
{
2931
if (! $this->option('force')) {
30-
$this->newLine(2);
31-
32-
$this->info("Running the install will reset all of the application's data.");
33-
$this->warn('!!! ALL OF THE DATA WILL BE DELETED !!!');
34-
35-
if (! $this->confirm('Do you wish to continue?')) {
36-
$this->info('Install cancelled.');
32+
$confirmed = confirm('Are you sure you want to continue?');
3733

38-
return;
34+
if (! $confirmed) {
35+
$this->fail('Application install cancelled.');
3936
}
4037
}
4138

42-
$this->info('Starting to install the application...');
43-
44-
$this->newLine();
45-
46-
$this->checkAppKey();
47-
48-
$this->line('⏳ Optimizing the cache...');
39+
$this->info('⏳ Starting to install the application...');
4940

5041
if (app()->environment('production') || app()->environment('testing')) {
51-
Artisan::call('view:clear');
52-
Artisan::call('filament:cache-components');
53-
Artisan::call('optimize');
42+
$this->call('filament:cache-components');
43+
$this->call('optimize');
44+
} else {
45+
$this->call('optimize:clear');
5446
}
5547

56-
$this->line('✅ Optimized cache');
57-
58-
$this->newLine();
59-
60-
$this->line('⏳ Migrating the database...');
61-
6248
try {
63-
Artisan::call('migrate:fresh', [
49+
$this->call('migrate:fresh', [
6450
'--force' => true,
6551
]);
6652
} catch (\Throwable $th) {
67-
$this->error('❌ There was an issue migrating the database, check the logs.');
68-
69-
return;
53+
$this->fail('❌ There was an issue migrating the database, check the logs.');
7054
}
7155

72-
$this->line('✅ Database migrated');
73-
74-
$this->newLine();
75-
76-
$this->line('🚀 Finished installing the application!');
77-
}
78-
79-
public function checkAppKey()
80-
{
81-
if (empty(config('app.key'))) {
82-
$this->line('🔑 Creating an application key');
83-
84-
Artisan::call('key:generate');
85-
86-
$this->line('✅ Application key created');
87-
}
56+
info('🚀 Finished installing Speedtest Tracker!');
8857
}
8958
}

app/Console/Commands/ResetUserPassword.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

app/Console/Commands/Maintenance/FixResultStatuses.php renamed to app/Console/Commands/ResultFixStatuses.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Illuminate\Database\Query\Builder;
88
use Illuminate\Support\Facades\DB;
99

10-
class FixResultStatuses extends Command
10+
class ResultFixStatuses extends Command
1111
{
1212
/**
1313
* The name and signature of the console command.
1414
*
1515
* @var string
1616
*/
17-
protected $signature = 'app:fix-result-statuses';
17+
protected $signature = 'app:result-fix-statuses';
1818

1919
/**
2020
* The console command description.
@@ -34,7 +34,7 @@ public function handle(): void
3434
$this->info('📖 Read the docs: https://docs.speedtest-tracker.dev/other/commands');
3535

3636
if (! $this->confirm('Do you want to continue?')) {
37-
return;
37+
$this->fail('Command cancelled.');
3838
}
3939

4040
/**

app/Console/Commands/SystemMaintenance.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

app/Console/Commands/UpdateGeneralSettings.php

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Illuminate\Console\Command;
99

1010
use function Laravel\Prompts\confirm;
11-
use function Laravel\Prompts\select;
1211
use function Laravel\Prompts\text;
1312

1413
class UpdateGeneralSettings extends Command
@@ -25,7 +24,7 @@ class UpdateGeneralSettings extends Command
2524
*
2625
* @var string
2726
*/
28-
protected $description = 'CLI to update the general settings.';
27+
protected $description = 'Update the application\'s general settings.';
2928

3029
/**
3130
* Execute the console command.
@@ -34,8 +33,6 @@ public function handle(): void
3433
{
3534
$settings = new GeneralSettings();
3635

37-
$this->updateSiteName($settings);
38-
$this->updatePublicDashboard($settings);
3936
$this->updateTimeZone($settings);
4037
$this->updateSchedule($settings);
4138
$this->resetSevers($settings);
@@ -59,26 +56,6 @@ protected function resetSevers($settings): void
5956
}
6057
}
6158

62-
protected function updatePublicDashboard($settings): void
63-
{
64-
$publicDashboard = select(
65-
label: 'Make the dashboard public?',
66-
options: ['Yes', 'No'],
67-
default: 'Yes',
68-
required: true,
69-
);
70-
71-
if ($publicDashboard == 'Yes') {
72-
$settings->public_dashboard_enabled = true;
73-
74-
$settings->save();
75-
} else {
76-
$settings->public_dashboard_enabled = false;
77-
}
78-
79-
$settings->save();
80-
}
81-
8259
protected function updateSchedule($settings): void
8360
{
8461
$cron = text(
@@ -99,27 +76,6 @@ protected function updateSchedule($settings): void
9976
}
10077
}
10178

102-
protected function updateSiteName($settings): void
103-
{
104-
$name = text(
105-
label: 'What is the site name?',
106-
placeholder: 'Speedtest Tracker',
107-
default: $settings->site_name,
108-
required: true,
109-
validate: fn (string $value) => match (true) {
110-
strlen($value) < 2 => 'The site name must be at least 2 characters.',
111-
strlen($value) > 50 => 'The site name must not exceed 50 characters.',
112-
default => null
113-
}
114-
);
115-
116-
if ($name) {
117-
$settings->site_name = $name;
118-
119-
$settings->save();
120-
}
121-
}
122-
12379
protected function updateTimeZone($settings): void
12480
{
12581
$timezone = text(
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@
55
use App\Models\User;
66
use Illuminate\Console\Command;
77

8-
use function Laravel\Prompts\confirm;
98
use function Laravel\Prompts\info;
109
use function Laravel\Prompts\select;
1110
use function Laravel\Prompts\text;
1211

13-
class UpdateUserRole extends Command
12+
class UserChangeRole extends Command
1413
{
1514
/**
1615
* The name and signature of the console command.
1716
*
1817
* @var string
1918
*/
20-
protected $signature = 'app:update-user-role';
19+
protected $signature = 'app:user-change-role';
2120

2221
/**
2322
* The console command description.
2423
*
2524
* @var string
2625
*/
27-
protected $description = 'Change the role for a given user.';
26+
protected $description = 'Change the role for a user.';
2827

2928
/**
3029
* Execute the console command.
@@ -49,18 +48,11 @@ public function handle(): void
4948
default: 'user'
5049
);
5150

52-
$confirmed = confirm(
53-
label: 'Are you sure?',
54-
required: true
55-
);
56-
57-
if ($confirmed) {
58-
User::firstWhere('email', $email)
59-
->update([
60-
'role' => $role,
61-
]);
51+
User::where('email', '=', $email)
52+
->update([
53+
'role' => $role,
54+
]);
6255

63-
info('User role updated.');
64-
}
56+
info('User role updated.');
6557
}
6658
}

0 commit comments

Comments
 (0)