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 @@