$root, 'config' => $config] = require __DIR__ . '/_bootstrap.php'; if ($_SERVER['REQUEST_METHOD'] !== 'POST') { Http::methodNotAllowed(['POST']); exit; } Security::validateCsrf(); (new RateLimiter($root, $config, RateLimiter::clientIp()))->check('score'); $maxBody = (int) ($config['max_body_bytes'] ?? 1_048_576); $body = Http::readJsonBody($maxBody > 0 ? $maxBody : null); $packId = (string) ($body['pack_id'] ?? 'onboarding-v1'); $answers = $body['answers'] ?? []; $optional = $body['optional'] ?? []; if (!is_array($answers) || !is_array($optional)) { Http::json(['error' => 'Invalid payload'], 400); exit; } try { $packRepo = new PackRepository($root); $rulesRepo = new RulesRepository($root); $pack = $packRepo->getPack($packId); $rules = $rulesRepo->getRules(); $result = Engine::compute($pack, $answers, $optional, $rules); (new Analytics($root))->track(Analytics::EVENT_DIA_COMPLETED, $packId); // Notification admin via webhook $webhookUrl = (string) ($config['admin_notify_webhook'] ?? ''); if ($webhookUrl !== '' && filter_var($webhookUrl, FILTER_VALIDATE_URL)) { $pdfBytes = (new PdfRenderer($root))->renderPdfBytes($pack, $result, [ 'booking_url' => '', 'date' => date('Y-m-d'), ]); $filename = 'controle-technique-' . ($pack['pack_id'] ?? 'resultat') . '.pdf'; $ctx = ['pack_id' => $packId, 'answers' => $answers, 'optional' => $optional, 'result' => $result]; (new AdminNotifier($webhookUrl, $root . '/storage/outbox'))->notify('score', $ctx, $filename, $pdfBytes, null); } Http::json(['result' => $result]); } catch (Throwable $e) { Http::json(['error' => $e->getMessage()], 400); }