From c8c2f1eee4fdb89e7e8dabd170d38ea7e7266073 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 8 Jun 2024 12:56:00 +0400 Subject: [PATCH 1/7] refactor OrderProduct model to use enums for cup size and milk type and also made neccesary modifications in others files --- src/models/Order.php | 4 ++-- src/models/OrderCupSize.php | 12 ++++++++++++ src/models/OrderMilkType.php | 14 ++++++++++++++ src/models/OrderProduct.php | 32 +++++++++++++++++-------------- tests/models/OrderProductTest.php | 12 +++++++++--- tests/models/OrderTest.php | 14 +++++++++++--- 6 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 src/models/OrderCupSize.php create mode 100644 src/models/OrderMilkType.php diff --git a/src/models/Order.php b/src/models/Order.php index 07377be..a41a89a 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -302,8 +302,8 @@ public static function getOrderProducts(int $order_id): array foreach ($data as $result) { $order_products_arr[] = new OrderProduct( product_id: $result->product_id, - cup_size: $result->cup_size, - milk_type: $result->milk_type, + cup_size: OrderCupSize::from($result->cup_size), // Convert string to enum + milk_type: OrderMilkType::from($result->milk_type), // Convert string to enum quantity: $result->quantity, unit_price: (float)$result->unit_price, order_id: $result->order_id, diff --git a/src/models/OrderCupSize.php b/src/models/OrderCupSize.php new file mode 100644 index 0000000..359744b --- /dev/null +++ b/src/models/OrderCupSize.php @@ -0,0 +1,12 @@ +milk_type, ['almond', 'coconut', 'oat', 'soy'])) { + // Validate milk type using enum values + if (!in_array($this->milk_type, [OrderMilkType::ALMOND, OrderMilkType::COCONUT, OrderMilkType::OAT, OrderMilkType::SOY])) { $errors['milk_type'] = 'Milk type invalid'; } - if (!in_array($this->cup_size, ['small', 'medium', 'large'])) { + // Validate cup size using enum values + if (!in_array($this->cup_size, [OrderCupSize::SMALL, OrderCupSize::MEDIUM, OrderCupSize::LARGE])) { $errors['cup_size'] = 'Cup size type invalid'; } @@ -130,12 +134,12 @@ public function getProductName(): string return Product::getByID($this->product_id)->getName(); } - public function getCupSize(): string + public function getCupSize(): OrderCupSize { return $this->cup_size; } - public function getMilkType(): string + public function getMilkType(): OrderMilkType { return $this->milk_type; } @@ -160,12 +164,12 @@ public function setProductID(int $product_id): void $this->product_id = $product_id; } - public function setCupSize(string $cup_size): void + public function setCupSize(OrderCupSize $cup_size): void { $this->cup_size = $cup_size; } - public function setMilkType(string $milk_type): void + public function setMilkType(OrderMilkType $milk_type): void { $this->milk_type = $milk_type; } @@ -185,8 +189,8 @@ public function toArray(): array return [ 'order_id' => $this->order_id, 'product_id' => $this->product_id, - 'cup_size' => $this->cup_size, - 'milk_type' => $this->milk_type, + 'cup_size' => $this->cup_size->value, + 'milk_type' => $this->milk_type->value, 'quantity' => $this->quantity, 'unit_price' => $this->unit_price, ]; diff --git a/tests/models/OrderProductTest.php b/tests/models/OrderProductTest.php index 69563b6..6e190f0 100644 --- a/tests/models/OrderProductTest.php +++ b/tests/models/OrderProductTest.php @@ -11,6 +11,8 @@ use Steamy\Model\OrderProduct; use Steamy\Model\Product; use Steamy\Model\Store; +use Steamy\Model\OrderMilkType; +use Steamy\Model\OrderCupSize; use Steamy\Tests\helpers\TestHelper; use Throwable; @@ -61,7 +63,11 @@ public function setUp(): void // Create dummy order line items $this->line_items = [ new OrderProduct( - $this->dummy_product->getProductID(), "medium", "oat", 2 + product_id: $this->dummy_product->getProductID(), + cup_size: OrderCupSize::MEDIUM, + milk_type: OrderMilkType::OAT, + quantity: 2, + unit_price: 5.0 ) ]; @@ -98,8 +104,8 @@ public function testValidate(): void { $invalidOrderProduct = new OrderProduct( product_id: $this->dummy_product->getProductID(), - cup_size: "extra large", // Invalid cup size - milk_type: "invalid milk", // Invalid milk type + cup_size: OrderCupSize::LARGE, + milk_type: OrderMilkType::SOY, quantity: -1, // Invalid quantity unit_price: -2.99, // Invalid unit price order_id: $this->dummy_order->getOrderID() diff --git a/tests/models/OrderTest.php b/tests/models/OrderTest.php index b7a9173..5b164fd 100644 --- a/tests/models/OrderTest.php +++ b/tests/models/OrderTest.php @@ -15,6 +15,8 @@ use Steamy\Model\Product; use Steamy\Model\Store; use Steamy\Tests\helpers\TestHelper; +use Steamy\Model\OrderMilkType; +use Steamy\Model\OrderCupSize; class OrderTest extends TestCase { @@ -112,8 +114,8 @@ public function setUp(): void // Create dummy order line items $this->line_items = [ - new OrderProduct($product1->getProductID(), "medium", "oat", 2, 5.0), - new OrderProduct($product2->getProductID(), "small", "almond", 1, 3.0) + new OrderProduct($product1->getProductID(), OrderCupSize::MEDIUM, OrderMilkType::OAT, 2, 5.0), + new OrderProduct($product2->getProductID(), OrderCupSize::SMALL, OrderMilkType::ALMOND, 1, 3.0) ]; // Create a dummy order @@ -201,7 +203,13 @@ public function testSaveWithEmptyLineItems(): void public function testAddLineItem(): void { $order = new Order($this->dummy_store->getStoreID(), $this->client->getUserID()); - $order->addLineItem(new OrderProduct(1, "medium", "oat", 1, 5.0)); + $order->addLineItem(new OrderProduct( + product_id: 1, + cup_size: OrderCupSize::MEDIUM, + milk_type: OrderMilkType::OAT, + quantity: 1, + unit_price: 5.0 + )); self::assertCount(1, $order->getLineItems()); } From a33383fd2fab412990a05ebe5f4d7fcf76d1e267 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 8 Jun 2024 14:43:54 +0400 Subject: [PATCH 2/7] update assertions in OrderProductTest to use enum values instead of strings for cup size and milk type --- tests/models/OrderProductTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/models/OrderProductTest.php b/tests/models/OrderProductTest.php index 6e190f0..e571c62 100644 --- a/tests/models/OrderProductTest.php +++ b/tests/models/OrderProductTest.php @@ -130,8 +130,8 @@ public function testGetById(): void $this->assertNotNull($retrievedOrderProduct); $this->assertEquals($this->dummy_order->getOrderID(), $retrievedOrderProduct->getOrderID()); $this->assertEquals($this->dummy_product->getProductID(), $retrievedOrderProduct->getProductID()); - $this->assertEquals("medium", $retrievedOrderProduct->getCupSize()); - $this->assertEquals("oat", $retrievedOrderProduct->getMilkType()); + $this->assertEquals(OrderCupSize::MEDIUM, $retrievedOrderProduct->getCupSize()); + $this->assertEquals(OrderMilkType::OAT, $retrievedOrderProduct->getMilkType()); $this->assertEquals(2, $retrievedOrderProduct->getQuantity()); $this->assertEquals($this->dummy_product->getPrice(), $retrievedOrderProduct->getUnitPrice()); } From 2f796ce2ebbeeb8a0614b6d5e6e41313bab17314 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 8 Jun 2024 15:47:43 +0400 Subject: [PATCH 3/7] update Cart controller to use match statements for enum conversions and update Orders view to display milk type and cup size values from enum objects --- src/controllers/Cart.php | 17 +++++++++++++++-- src/views/Orders.php | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/controllers/Cart.php b/src/controllers/Cart.php index 58588ad..2f4f78d 100644 --- a/src/controllers/Cart.php +++ b/src/controllers/Cart.php @@ -12,6 +12,8 @@ use Steamy\Model\OrderProduct; use Steamy\Model\Product; use Steamy\Model\Store; +use Steamy\Model\OrderMilkType; +use Steamy\Model\OrderCupSize; class Cart { @@ -101,8 +103,19 @@ 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: strtolower($item['cupSize']), - milk_type: strtolower($item['milkType']), + 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']) + }, quantity: filter_var($item['quantity'], FILTER_VALIDATE_INT) ); $new_order->addLineItem($line_item); diff --git a/src/views/Orders.php b/src/views/Orders.php index a51e937..f9b5cef 100644 --- a/src/views/Orders.php +++ b/src/views/Orders.php @@ -37,8 +37,8 @@ getProductName()) ?> getQuantity(), FILTER_SANITIZE_NUMBER_INT) ?> - getMilkType()) ?> - getCupSize()) ?> + getMilkType()->value)) ?> + getCupSize()->value)) ?> $getUnitPrice(), 2)) ?> Date: Tue, 9 Jul 2024 17:55:40 +0400 Subject: [PATCH 4/7] update based on new enums --- tests/helpers/TestHelper.php | 4 +++- tests/models/OrderProductTest.php | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/helpers/TestHelper.php b/tests/helpers/TestHelper.php index 661c177..1922664 100644 --- a/tests/helpers/TestHelper.php +++ b/tests/helpers/TestHelper.php @@ -10,6 +10,8 @@ use Steamy\Model\Client; use Steamy\Model\Location; use Steamy\Model\Order; +use Steamy\Model\OrderCupSize; +use Steamy\Model\OrderMilkType; use Steamy\Model\OrderProduct; use Steamy\Model\Product; use Faker\Generator; @@ -244,7 +246,7 @@ public static function createReview( $store->addProductStock($product->getProductID(), 10); $order = new Order($store->getStoreID(), $client->getUserID(), [ - new OrderProduct($product->getProductID(), 'small', 'oat', 1) + new OrderProduct($product->getProductID(), OrderCupSize::LARGE, OrderMilkType::ALMOND, 1) ]); $success = $order->save(); diff --git a/tests/models/OrderProductTest.php b/tests/models/OrderProductTest.php index e571c62..d2461a1 100644 --- a/tests/models/OrderProductTest.php +++ b/tests/models/OrderProductTest.php @@ -114,8 +114,6 @@ public function testValidate(): void $errors = $invalidOrderProduct->validate(); $this->assertArrayHasKey('quantity', $errors); - $this->assertArrayHasKey('cup_size', $errors); - $this->assertArrayHasKey('milk_type', $errors); $this->assertArrayHasKey('unit_price', $errors); } From 0baa82622f20c730f30fcfa5c7ff6c9517117af2 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:26:20 +0400 Subject: [PATCH 5/7] send email on reorder --- src/controllers/Profile.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/controllers/Profile.php b/src/controllers/Profile.php index e518acb..0d889ce 100644 --- a/src/controllers/Profile.php +++ b/src/controllers/Profile.php @@ -104,6 +104,9 @@ private function displayProfileDetails(Client $client, string $password = "", st ); } + /** + * @throws Exception + */ public function reorderOrder(): void { $order_id = (int)($_POST['order_id'] ?? -1); @@ -127,6 +130,8 @@ public function reorderOrder(): void } catch (Exception $e) { $this->view_data['order_action_error'] = $e->getMessage(); } + + $this->signed_client->sendOrderConfirmationEmail($new_order); } public function cancelOrder(): void From 2296bc69752bae5811f749e8e3dfbecbffeb7895 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:28:05 +0400 Subject: [PATCH 6/7] use enum --- src/models/Order.php | 4 +-- src/models/OrderProduct.php | 20 +++----------- src/views/Contact.php | 38 ++++++++++++++++++--------- src/views/mails/OrderConfirmation.php | 7 +++-- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index a41a89a..deccba7 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -33,7 +33,7 @@ public function __construct( array $line_items = [], ?int $order_id = null, ?DateTime $pickup_date = null, - OrderStatus $status = OrderStatus::PENDING, // Default to 'pending', + OrderStatus $status = OrderStatus::PENDING, DateTime $created_date = new DateTime(), ) { $this->store_id = $store_id; @@ -273,7 +273,7 @@ public function cancelOrder(): void $stm->execute(['order_id' => $this->order_id]); $conn->commit(); - } catch (PDOException $e) { + } catch (PDOException) { $conn->rollBack(); } finally { $conn = null; diff --git a/src/models/OrderProduct.php b/src/models/OrderProduct.php index 5f66612..f00eb04 100644 --- a/src/models/OrderProduct.php +++ b/src/models/OrderProduct.php @@ -6,8 +6,6 @@ use Exception; use Steamy\Core\Model; -use Steamy\Model\OrderMilkType; -use Steamy\Model\OrderCupSize; class OrderProduct { @@ -74,16 +72,6 @@ public function validate(): array $errors['quantity'] = 'Quantity must be a positive integer'; } - // Validate milk type using enum values - if (!in_array($this->milk_type, [OrderMilkType::ALMOND, OrderMilkType::COCONUT, OrderMilkType::OAT, OrderMilkType::SOY])) { - $errors['milk_type'] = 'Milk type invalid'; - } - - // Validate cup size using enum values - if (!in_array($this->cup_size, [OrderCupSize::SMALL, OrderCupSize::MEDIUM, OrderCupSize::LARGE])) { - $errors['cup_size'] = 'Cup size type invalid'; - } - if ($this->unit_price <= 0) { $errors['unit_price'] = 'Unit price cannot be negative'; } @@ -111,8 +99,8 @@ public static function getByID(int $order_id, int $product_id): ?OrderProduct return new OrderProduct( product_id: $result->product_id, - cup_size: $result->cup_size, - milk_type: $result->milk_type, + cup_size: OrderCupSize::from($result->cup_size), + milk_type: OrderMilkType::from($result->milk_type), quantity: $result->quantity, unit_price: (float)$result->unit_price, order_id: $result->order_id, @@ -189,8 +177,8 @@ public function toArray(): array return [ 'order_id' => $this->order_id, 'product_id' => $this->product_id, - 'cup_size' => $this->cup_size->value, - 'milk_type' => $this->milk_type->value, + 'cup_size' => $this->cup_size->value, + 'milk_type' => $this->milk_type->value, 'quantity' => $this->quantity, 'unit_price' => $this->unit_price, ]; diff --git a/src/views/Contact.php b/src/views/Contact.php index 8a242a2..4d74a91 100644 --- a/src/views/Contact.php +++ b/src/views/Contact.php @@ -9,6 +9,7 @@ * @var string $defaultLastName Default value for last name input field * @var string $defaultEmail Default value for email input field * @var string $defaultMessage Default value for message textarea + * @var bool $contact_us_successful Whether contact was successful * @var array $errors Array of validation errors */ @@ -19,28 +20,40 @@

Contact Us

- - + + - + - - + + - + - + - + - - + + - +
@@ -50,7 +63,8 @@ >

Thank You for Contacting Us! 🔎

-

Your message has been successfully sent. Our team will review your inquiry and get back to you shortly. We appreciate your interest in our services.

+

Your message has been successfully sent. Our team will review your inquiry and get back to you shortly. We + appreciate your interest in our services.

diff --git a/src/views/mails/OrderConfirmation.php b/src/views/mails/OrderConfirmation.php index ff7fbed..541557a 100644 --- a/src/views/mails/OrderConfirmation.php +++ b/src/views/mails/OrderConfirmation.php @@ -16,7 +16,6 @@ */ -use Steamy\Model\Client; use Steamy\Model\Order; use Steamy\Model\Store; @@ -66,10 +65,10 @@ $quantity = $orderProduct->getQuantity(); $pricePerUnit = $orderProduct->getUnitPrice(); $subtotal = $pricePerUnit * $quantity; - $size = htmlspecialchars(ucfirst($orderProduct->getCupSize())); + $size = htmlspecialchars(ucfirst($orderProduct->getCupSize()->value)); $total += $subtotal; $milk = htmlspecialchars( - ucfirst($orderProduct->getMilkType()) + ucfirst($orderProduct->getMilkType()->value) ); echo <<< HTML @@ -91,7 +90,7 @@ -

Your order is now being processed and you will receive a notification once your order is ready. If you have any +

Your order is now being processed, and you will receive a notification once your order is ready. If you have any questions, feel free to call our store at getPhoneNo() ?>.

Best Regards,

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 7/7] 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 @@ New Contact Message -
-

New Contact Message

-

Name:

-

Email:

-

Message:

-

- +
+

New Contact Message

+

Name:

+

Email:

+

Message:

+

+ +