feat(*.php, *.psql): deduplicate API code + performance improvements

This commit is contained in:
Cory Dransfeldt 2025-04-22 12:39:42 -07:00
parent cf3dac8a46
commit 4bad005e58
No known key found for this signature in database
31 changed files with 502 additions and 666 deletions

View file

@ -15,17 +15,9 @@ class ContactHandler extends BaseHandler
public function __construct(?Client $httpClient = null)
{
parent::__construct();
$this->httpClient = $httpClient ?? new Client();
$this->loadEnvironment();
}
private function loadEnvironment(): void
{
$this->postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
$this->postgrestApiKey =
$_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
$this->forwardEmailApiKey =
$_ENV["FORWARDEMAIL_API_KEY"] ?? getenv("FORWARDEMAIL_API_KEY");
$this->forwardEmailApiKey = $_ENV["FORWARDEMAIL_API_KEY"] ?? getenv("FORWARDEMAIL_API_KEY");
}
public function handleRequest(): void
@ -42,7 +34,7 @@ class ContactHandler extends BaseHandler
$rawBody = file_get_contents("php://input");
$formData = json_decode($rawBody, true);
if (!$formData || !isset($formData["data"])) {
throw new Exception("Invalid JSON payload.");
throw new \Exception("Invalid JSON payload.");
}
$formData = $formData["data"];
} elseif (
@ -93,7 +85,7 @@ class ContactHandler extends BaseHandler
$this->saveToDatabase($contactData);
$this->sendNotificationEmail($contactData);
$this->sendRedirect("/contact/success");
} catch (Exception $e) {
} catch (\Exception $e) {
error_log("Error handling contact form submission: " . $e->getMessage());
$this->sendErrorResponse($e->getMessage(), 400);
}
@ -103,7 +95,7 @@ class ContactHandler extends BaseHandler
{
$referer = $_SERVER["HTTP_REFERER"] ?? "";
$allowedDomain = "coryd.dev";
if (!str_contains($referer, $allowedDomain)) throw new Exception("Invalid submission origin.");
if (!str_contains($referer, $allowedDomain)) throw new \Exception("Invalid submission origin.");
}
private function checkRateLimit(): void
@ -132,7 +124,7 @@ class ContactHandler extends BaseHandler
private function enforceHttps(): void
{
if (empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== "on") throw new Exception("Secure connection required. Use HTTPS.");
if (empty($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== "on") throw new \Exception("Secure connection required. Use HTTPS.");
}
private function isBlockedDomain(string $email): bool
@ -171,7 +163,7 @@ class ContactHandler extends BaseHandler
if ($response->getStatusCode() >= 400) {
$errorResponse = json_decode($response->getBody(), true);
throw new Exception(
throw new \Exception(
"PostgREST error: " . ($errorResponse["message"] ?? "Unknown error")
);
}
@ -204,7 +196,7 @@ class ContactHandler extends BaseHandler
]
);
if ($response->getStatusCode() >= 400) throw new Exception("Failed to send email notification.");
if ($response->getStatusCode() >= 400) throw new \Exception("Failed to send email notification.");
}
private function sendRedirect(string $path): void
@ -221,7 +213,7 @@ class ContactHandler extends BaseHandler
try {
$handler = new ContactHandler();
$handler->handleRequest();
} catch (Exception $e) {
} catch (\Exception $e) {
error_log("Contact form error: " . $e->getMessage());
echo json_encode(["error" => $e->getMessage()]);
http_response_code(500);