From 4d094c4355fff0a8a565b78a4ea7e0452bddcc14 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:28:35 +0400 Subject: [PATCH] refactor --- src/controllers/API.php | 2 +- src/controllers/Cart.php | 16 ++------ src/views/mails/Contact.php | 79 +++++++++++++++++++++---------------- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/controllers/API.php b/src/controllers/API.php index a8afffd..6c547d6 100644 --- a/src/controllers/API.php +++ b/src/controllers/API.php @@ -67,7 +67,7 @@ private function getHandler(string $controllerName): ?string ); // Replace placeholders with regex capture groups $pattern = '/^' . $pattern . '$/'; - if (preg_match($pattern, '/' . Utility::getURL(), $matches)) { + if (preg_match($pattern, '/' . Utility::getURL())) { return $handler; } } diff --git a/src/controllers/Cart.php b/src/controllers/Cart.php index 2f4f78d..256ce38 100644 --- a/src/controllers/Cart.php +++ b/src/controllers/Cart.php @@ -103,19 +103,8 @@ private function handleCheckout(): void foreach ($form_data['items'] as $item) { $line_item = new OrderProduct( product_id: filter_var($item['productID'], FILTER_VALIDATE_INT), - cup_size: match (strtolower($item['cupSize'])) { // Using match for enum conversion - 'small' => OrderCupSize::SMALL, - 'medium' => OrderCupSize::MEDIUM, - 'large' => OrderCupSize::LARGE, - default => throw new Exception('Invalid cup size: ' . $item['cupSize']) - }, - milk_type: match (strtolower($item['milkType'])) { // Using match for enum conversion - 'almond' => OrderMilkType::ALMOND, - 'coconut' => OrderMilkType::COCONUT, - 'oat' => OrderMilkType::OAT, - 'soy' => OrderMilkType::SOY, - default => throw new Exception('Invalid milk type: ' . $item['milkType']) - }, + cup_size: OrderCupSize::from(strtolower($item['cupSize'])), + milk_type: OrderMilkType::from(strtolower($item['milkType'])), quantity: filter_var($item['quantity'], FILTER_VALIDATE_INT) ); $new_order->addLineItem($line_item); @@ -154,6 +143,7 @@ private function handleCheckout(): void if (!$success_mail) { http_response_code(503); echo json_encode(['error' => "Order was saved but email could not be sent for an unknown reason."]); + return; } // if everything is good, tell client to reset the document view diff --git a/src/views/mails/Contact.php b/src/views/mails/Contact.php index 3bf7c33..c3472bf 100644 --- a/src/views/mails/Contact.php +++ b/src/views/mails/Contact.php @@ -1,7 +1,14 @@ @@ -10,42 +17,46 @@
Name: = htmlspecialchars($first_name) ?> = htmlspecialchars($last_name) ?>
-Email: = htmlspecialchars($email) ?>
-Message:
-= nl2br(htmlspecialchars($message)) ?>
- +Name: = htmlspecialchars($first_name) ?> = htmlspecialchars($last_name) ?>
+Email: = htmlspecialchars($email) ?>
+Message:
+= nl2br(htmlspecialchars($message)) ?>
+ +