From f478038644e2c59e788a50bd32dc82477f8e05df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 29 Sep 2022 15:25:42 +0200 Subject: [PATCH] improve code --- src/Commands/Down.php | 28 +++---------------- src/Commands/Up.php | 23 ++------------- src/Database/Concerns/MaintenanceMode.php | 7 +++-- .../CheckTenantForMaintenanceMode.php | 2 +- tests/MaintenanceModeTest.php | 4 +-- 5 files changed, 15 insertions(+), 49 deletions(-) diff --git a/src/Commands/Down.php b/src/Commands/Down.php index 6b01d149..23598212 100644 --- a/src/Commands/Down.php +++ b/src/Commands/Down.php @@ -11,31 +11,16 @@ class Down extends DownCommand { use HasATenantsOption; - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'tenancy:down + protected $signature = 'tenants:down {--redirect= : The path that users should be redirected to} {--retry= : The number of seconds after which the request may be retried} {--refresh= : The number of seconds after which the browser may refresh} {--secret= : The secret phrase that may be used to bypass maintenance mode} {--status=503 : The status code that should be used when returning the maintenance mode response}'; - /** - * The console command description. - * - * @var string - */ - protected $description = 'Put tenants into maintenance'; + protected $description = 'Put tenants into maintenance mode.'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() + public function handle(): void { // The base down command is heavily used. Instead of saving the data inside a file, // the data is stored the tenant database, which means some Laravel features @@ -52,12 +37,7 @@ class Down extends DownCommand $this->comment('Tenants are now in maintenance mode.'); } - - /** - * Get the payload to be placed in the "down" file. - * - * @return array - */ + /** Get the payload to be placed in the "down" file. */ protected function getDownDatabasePayload() { return [ diff --git a/src/Commands/Up.php b/src/Commands/Up.php index 91731ada..6e68a02a 100644 --- a/src/Commands/Up.php +++ b/src/Commands/Up.php @@ -11,30 +11,13 @@ class Up extends Command { use HasATenantsOption; - /** - * The name and signature of the console command. - * - * @var string - */ + protected $signature = 'tenants:up'; - protected $signature = 'tenancy:up'; - - /** - * The console command description. - * - * @var string - */ protected $description = 'Put tenants out of maintenance'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() + public function handle(): void { - // This runs for all tenants if no --tenants are specified - tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { + tenancy()->runForMultiple($this->getTenants(), function ($tenant) { $this->line("Tenant: {$tenant['id']}"); $tenant->bringUpFromMaintenance(); }); diff --git a/src/Database/Concerns/MaintenanceMode.php b/src/Database/Concerns/MaintenanceMode.php index a4e5c465..44b0a45f 100644 --- a/src/Database/Concerns/MaintenanceMode.php +++ b/src/Database/Concerns/MaintenanceMode.php @@ -4,9 +4,12 @@ declare(strict_types=1); namespace Stancl\Tenancy\Database\Concerns; +/** + * @mixin \Illuminate\Database\Eloquent\Model + */ trait MaintenanceMode { - public function putDownForMaintenance($data = []) + public function putDownForMaintenance($data = []): void { $this->update([ 'maintenance_mode' => [ @@ -20,7 +23,7 @@ trait MaintenanceMode ]); } - public function bringUpFromMaintenance() + public function bringUpFromMaintenance(): void { $this->update(['maintenance_mode' => null]); } diff --git a/src/Middleware/CheckTenantForMaintenanceMode.php b/src/Middleware/CheckTenantForMaintenanceMode.php index bbc29373..32e90dc8 100644 --- a/src/Middleware/CheckTenantForMaintenanceMode.php +++ b/src/Middleware/CheckTenantForMaintenanceMode.php @@ -24,7 +24,7 @@ class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode return $this->bypassResponse($data['secret']); } - if (version_compare($this->app->version(), '8.0.0') >= 0 && $this->hasValidBypassCookie($request, $data) || + if (version_compare($this->app->version(), '8.0.0', '>=') && $this->hasValidBypassCookie($request, $data) || $this->inExceptArray($request)) { return $next($request); } diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php index 4df495e9..98fadbe0 100644 --- a/tests/MaintenanceModeTest.php +++ b/tests/MaintenanceModeTest.php @@ -55,13 +55,13 @@ test('tenant can be in maintenance mode from command', function() { tenancy()->end(); // Flush stored tenant instance - Artisan::call('tenancy:down'); + Artisan::call('tenants:down'); pest()->get('http://acme.localhost/foo')->assertStatus(503); tenancy()->end(); - Artisan::call('tenancy:up'); + Artisan::call('tenants:up'); tenancy()->end();