43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Queue\Jobs;
|
|
|
|
use App\Services\RiskAnalysisService;
|
|
use App\Core\Database;
|
|
use Throwable;
|
|
|
|
final class RiskAnalysisJob
|
|
{
|
|
public function __construct(
|
|
private readonly RiskAnalysisService $riskService
|
|
) {}
|
|
|
|
public function handle(array $payload): void
|
|
{
|
|
$companyId = $payload['company_id'];
|
|
$tenantId = $payload['tenant_id'];
|
|
|
|
try {
|
|
$analysis = $this->riskService->calculateCompanyRiskScore($companyId);
|
|
|
|
// Store risk score
|
|
$db = Database::getInstance();
|
|
|
|
$stmt = $db->prepare("INSERT INTO risk_scores (id, tenant_id, company_id, risk_type, score, reason) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
\Ramsey\Uuid\Uuid::uuid4()->toString(),
|
|
$tenantId,
|
|
$companyId,
|
|
$analysis['level'], // risk_type = high/medium/low
|
|
$analysis['score'],
|
|
json_encode($analysis['factors'], JSON_UNESCAPED_UNICODE), // reason
|
|
]);
|
|
} catch (Throwable $e) {
|
|
echo "[!] Risk Analysis failed for company {$companyId}: " . $e->getMessage() . "\n";
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|