1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-15 03:24:03 +00:00
tenancy/src/Commands/CreatePendingTenants.php
Abrar Ahmad 68de3600bd
Improve commands CLI output (#1030)
* use component info/error methods

* Update src/Commands/ClearPendingTenants.php

Co-authored-by: lukinovec <lukinovec@gmail.com>

Co-authored-by: lukinovec <lukinovec@gmail.com>
2022-12-14 15:08:00 +01:00

46 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
class CreatePendingTenants extends Command
{
protected $signature = 'tenants:pending-create {--count= : The number of pending tenants to be created}';
protected $description = 'Create pending tenants.';
public function handle(): int
{
$this->components->info('Creating pending tenants.');
$maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count'));
$pendingTenantCount = $this->getPendingTenantCount();
$createdCount = 0;
while ($pendingTenantCount < $maxPendingTenantCount) {
tenancy()->model()::createPending();
// Fetching the pending tenant count in each iteration prevents creating too many tenants
// If pending tenants are being created somewhere else while running this command
$pendingTenantCount = $this->getPendingTenantCount();
$createdCount++;
}
$this->components->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.');
$this->components->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
return 0;
}
/** Calculate the number of currently available pending tenants. */
protected function getPendingTenantCount(): int
{
return tenancy()->query()
->onlyPending()
->count();
}
}