ldc-showroom-bo/app/Console/Commands/ExportShowRoom.php

83 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2024-01-08 15:28:00 +01:00
<?php
namespace App\Console\Commands;
use App\Jobs\ShowroomExport;
2025-05-20 12:51:38 +02:00
use App\Models\Tenant;
2024-01-08 15:28:00 +01:00
use Illuminate\Console\Command;
2025-05-20 12:51:38 +02:00
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use RuntimeException;
2024-01-08 15:28:00 +01:00
2025-05-20 12:51:38 +02:00
class ExportShowRoom extends Command implements PromptsForMissingInput
2024-01-08 15:28:00 +01:00
{
/**
* 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';
2025-05-20 12:51:38 +02:00
/**
* The tenant_id of the tenant that contains the datas to export.
*
* @var string|null
*/
protected $tenantId;
2024-01-08 15:28:00 +01:00
/**
* Execute the console command.
*
* @return void
2025-05-20 12:51:38 +02:00
*
* @throws RuntimeException If tenantId isn't a string. Should never happen but it makes intelephense happy.
2024-01-08 15:28:00 +01:00
*/
public function handle(): void
{
2025-05-20 12:51:38 +02:00
$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(
2025-05-20 12:51:38 +02:00
'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
2024-01-08 15:28:00 +01:00
}
}