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 58588ad..256ce38 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,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: strtolower($item['cupSize']), - milk_type: strtolower($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); @@ -141,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/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 diff --git a/src/models/Order.php b/src/models/Order.php index 07377be..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; @@ -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'])) { - $errors['milk_type'] = 'Milk type invalid'; - } - - if (!in_array($this->cup_size, ['small', 'medium', 'large'])) { - $errors['cup_size'] = 'Cup size type invalid'; - } - if ($this->unit_price <= 0) { $errors['unit_price'] = 'Unit price cannot be negative'; } @@ -107,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, @@ -130,12 +122,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 +152,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 +177,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/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/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)) ?> @@ -10,42 +17,46 @@ New Contact Message -
-

New Contact Message

-

Name:

-

Email:

-

Message:

-

- +
+

New Contact Message

+

Name:

+

Email:

+

Message:

+

+ +
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,

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 69563b6..d2461a1 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() @@ -108,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); } @@ -124,8 +128,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()); } 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()); }