Skip to content

Commit

Permalink
use enum
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 9, 2024
1 parent 0baa826 commit 2296bc6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 4 additions & 16 deletions src/models/OrderProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Exception;
use Steamy\Core\Model;
use Steamy\Model\OrderMilkType;
use Steamy\Model\OrderCupSize;

class OrderProduct
{
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
];
Expand Down
38 changes: 26 additions & 12 deletions src/views/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -19,28 +20,40 @@
<h1>Contact Us</h1>
<form action="" method="post">
<label for="firstname">First name</label>
<input type="text" id="firstname" name="first_name" value="<?= htmlspecialchars($defaultFirstName) ?>" required>
<?php if (isset($errors['first_name'])) : ?>
<input type="text" id="firstname" name="first_name" value="<?= htmlspecialchars($defaultFirstName) ?>"
required>
<?php
if (isset($errors['first_name'])) : ?>
<small class="warning"><?= $errors['first_name'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="lastname">Last name</label>
<input type="text" id="lastname" name="last_name" value="<?= htmlspecialchars($defaultLastName) ?>" required>
<?php if (isset($errors['last_name'])) : ?>
<input type="text" id="lastname" name="last_name" value="<?= htmlspecialchars($defaultLastName) ?>"
required>
<?php
if (isset($errors['last_name'])) : ?>
<small class="warning"><?= $errors['last_name'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="email">Your email</label>
<input type="email" id="email" name="email" value="<?= htmlspecialchars($defaultEmail) ?>" required>
<?php if (isset($errors['email'])) : ?>
<?php
if (isset($errors['email'])) : ?>
<small class="warning"><?= $errors['email'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="30" required><?= htmlspecialchars($defaultMessage) ?></textarea>
<?php if (isset($errors['message'])) : ?>
<textarea id="message" name="message" rows="5" cols="30" required><?= htmlspecialchars(
$defaultMessage
) ?></textarea>
<?php
if (isset($errors['message'])) : ?>
<small class="warning"><?= $errors['message'] ?></small>
<?php endif; ?>
<?php
endif; ?>

<input type="submit" name="form_submit" value="Submit">
</form>
Expand All @@ -50,7 +63,8 @@
<dialog <?= $contact_us_successful ? "open" : "" ?>>
<article>
<h3>Thank You for Contacting Us! 🔎</h3>
<p>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.</p>
<p>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.</p>
<footer>
<a href="/home" role="button" data-target="my-modal">Return to Home</a>
</footer>
Expand Down
7 changes: 3 additions & 4 deletions src/views/mails/OrderConfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/


use Steamy\Model\Client;
use Steamy\Model\Order;
use Steamy\Model\Store;

Expand Down Expand Up @@ -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
Expand All @@ -91,7 +90,7 @@
</tbody>
</table>

<p>Your order is now being processed and you will receive a notification once your order is ready. If you have any
<p>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 <?= $store->getPhoneNo() ?>.</p>

<p>Best Regards,</p>
Expand Down

0 comments on commit 2296bc6

Please sign in to comment.