ldc-showroom-bo/app/Console/Commands/ExportShowRoom.php
2025-05-27 09:11:43 +02:00

82 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\ShowroomExport;
use App\Models\Tenant;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use RuntimeException;
class ExportShowRoom extends Command implements PromptsForMissingInput
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'uwil:show-room:export';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Export showroom data : a zip file with json data and associated pictures';
/**
* The tenant_id of the tenant that contains the datas to export.
*
* @var string|null
*/
protected $tenantId;
/**
* Execute the console command.
*
* @return void
*
* @throws RuntimeException If tenantId isn't a string. Should never happen but it makes intelephense happy.
*/
public function handle(): void
{
$this->tenantId = null;
$this->addTenantId();
if (!$this->tenantId) {
throw new RuntimeException('\'tenantId\' must be a valid string.');
}
\dispatch_sync(new ShowroomExport($this->tenantId));
}
/**
* Add a role to the user.
*
* @return void
*/
private function addTenantId(): void
{
while (is_null($this->tenantId)) {
try {
$tmp = $this->choice(
'Select a tenant',
Tenant::all()->pluck('id')->toArray(),
Tenant::query()->first()->id,
);
$validator = Validator::make(['tenant_id' => $tmp], [
'tenant_id' => 'required'
]);
$validator->validate();
$this->tenantId = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
'Please choose from the selection %s',
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
} //end try
}; //end while
}
}