Files
portal-api/app/Jobs/Hosting/ProvisionHostingAccountJob.php
T
2026-08-14 08:45:52 +07:00

85 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs\Hosting;
use App\Enums\Server\ServerType;
use App\Models\DnsServer;
use App\Models\OrderItem;
use App\Services\Plesk\PleskHostingService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
/**
* Auto-provisions a Plesk hosting account when a hosting order is paid,
* mirroring CreateVirtualMachineJob's role for VPS orders. Reuses the same
* PleskHostingService::provision() the admin "create hosting account" form
* calls, so behavior (customer creation, subscription, notification emails)
* stays identical between manual and automatic provisioning.
*/
class ProvisionHostingAccountJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** Plesk subscription creation can take a couple of minutes */
public int $timeout = 300;
/** No auto-retry — hosting provisioning errors need manual review */
public int $tries = 1;
public function __construct(public OrderItem $item)
{
}
public function handle(PleskHostingService $plesk): void
{
$service = $this->item->service()->first();
$domainName = $this->item->server_name;
if (! $service || ! $domainName) {
Log::channel('Custom')->warning('ProvisionHostingAccountJob skipped: missing service or domain', [
'order_item_id' => $this->item->id,
]);
return;
}
$server = DnsServer::query()
->where('status', 'activated')
->whereIn('type', [ServerType::HOSTING->value, ServerType::BOTH->value])
->first();
if (! $server) {
Log::channel('Custom')->error('ProvisionHostingAccountJob failed: no active hosting server available', [
'order_item_id' => $this->item->id,
]);
return;
}
$result = $plesk->provision([
'customer_id' => $this->item->order->customer_id,
'service_id' => $service->id,
'dns_manager_server_id' => $server->id,
'domain_name' => $domainName,
'hosting_plan' => $this->item->product?->name ?? 'Default',
]);
if ($result['error']) {
Log::channel('Custom')->error('ProvisionHostingAccountJob failed', [
'order_item_id' => $this->item->id,
'message' => $result['message'] ?? 'Unknown error',
]);
return;
}
$service->update(['status' => 'active']);
}
}