Skip to content

Commit

Permalink
Merge branch 'creme332:main' into Incomplete-OrderModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyeshhhh authored May 8, 2024
2 parents f6b9bf3 + 831b4e9 commit 4834d5a
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/controllers/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/controllers/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Steamy\Controller;

use Steamy\Core\Controller;

class Error
{
use Controller;

private array $view_data;

public function __construct()
{
$this->view_data['error_message'] = '500 - Internal server error';
$this->view_data['extended_error_message'] = 'Something bad happened';
}

public function handleDatabaseError(): void
{
$this->view_data['extended_error_message'] = 'Unable to connect to database.';

$this->view(
'Error',
$this->view_data,
template_title: "Error",
enableIndexing: false
);
}

public function handlePageNotFoundError($extended_error_message = 'Ensure that you have properly typed the URL.'
): void {
$this->view_data['error_message'] = '404 - Page not found';
$this->view_data['extended_error_message'] = $extended_error_message;

$this->view(
'Error',
$this->view_data,
template_title: "Page not found",
enableIndexing: false
);
}

public function handleUnknownError(): void
{
$this->view_data['extended_error_message'] = 'The server has encountered a situation
it does not know how to handle.';

$this->view(
'Error',
$this->view_data,
template_title: "Unknown error",
enableIndexing: false
);
}

public function index(): void
{
$this->handlePageNotFoundError();
}
}
2 changes: 1 addition & 1 deletion src/controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function index(): void
}

// if url follows some other format display error page
(new _404())->index();
(new Error())->handlePageNotFoundError();
}
}

4 changes: 2 additions & 2 deletions src/controllers/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError('Product does not exist');
die();
}
}
Expand All @@ -254,7 +254,7 @@ public function index(): void

// if product was not found, display error page
if (empty($this->product)) {
(new _404())->index();
(new Error())->handlePageNotFoundError('Product does not exist.');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->handlePageNotFoundError();
die();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ private function handleFormSubmission(): void
Utility::redirect('login');
}

// TODO: redirect to some error page
Utility::redirect('home');
(new Error())->index("An error occurred while processing your registration. Please try again later.");
die();
} else {
$this->loadDataToForm($form_data);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ private function validateURL(): bool
private function handleInvalidURL(): void
{
if (!$this->validateURL()) {
(new _404())->index();
(new Error())->index("Page not found");
die();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function index(): void
// check if URL is not /shop
if (Utility::getURL() !== "shop") {
// let 404 controller handle this
(new _404())->index();
(new Error())->handlePageNotFoundError();
return;
}

Expand Down
24 changes: 0 additions & 24 deletions src/controllers/_404.php

This file was deleted.

22 changes: 19 additions & 3 deletions src/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

namespace Steamy\Core;

use Steamy\Controller\_404;
use Steamy\Controller\Error;
use Steamy\Controller\API;
use Throwable;

class App
{

/**
* Global exception handler
* @param Throwable $exception
* @return void
*/
public function exception_handler(Throwable $exception): void
{
// echo "Uncaught exception: ", $exception->getMessage(), "\n";
(new Error())->handleUnknownError();
}


/**
* Calls appropriate controller class to deal with URL.
* @return void
*/
public function loadController(): void
{
set_exception_handler(array($this, "exception_handler"));

$URL = Utility::splitURL();

switch ($URL[0]) {
Expand All @@ -28,8 +44,8 @@ public function loadController(): void
// call appropriate controller
(new $controllerClassName())->index();
} else {
// Fallback to 404 controller
(new _404())->index();
// Display error page
(new Error())->handlePageNotFoundError();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function view(
if (file_exists($view_file_path)) {
include $view_file_path;
} else {
include __DIR__ . '/../views/404.php';
include __DIR__ . '/../views/Error.php';
}
$template_content = ob_get_contents();
ob_end_clean();
Expand Down
4 changes: 2 additions & 2 deletions src/core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PDO;
use PDOException;
use stdClass;
use Steamy\Controller\Error;

trait Database
{
Expand All @@ -25,8 +26,7 @@ protected static function connect(): PDO
} catch (PDOException $e) {
// if PHPUnit is not running, handle the exception
if (!defined('PHPUNIT_STEAMY_TESTSUITE')) {
// TODO: Display a user-friendly error message
Utility::show("Sorry, we're unable to process your request at the moment. Please try again later.");
(new Error())->handleDatabaseError();
die();
} else {
// if PHPUnit is running, re-throw the exception to allow it to propagate
Expand Down
49 changes: 36 additions & 13 deletions src/models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function deleteUser(): void
public function save(): bool
{
// if attributes are invalid, exit
if (count($this->validate()) > 0) {
if (!empty($this->validate())) {
return false;
}

Expand All @@ -129,31 +129,54 @@ public function save(): bool
return false;
}

// get data to be inserted to user table
$user_data = parent::toArray();
unset($user_data['user_id']);
// start transaction
$conn = self::connect();
$conn->beginTransaction();

// perform insertion to user table
$inserted_id = $this->insert($user_data, 'user');
$query = <<< EOL
INSERT INTO user(email, first_name, password, phone_no, last_name)
VALUES(:email, :first_name, :password, :phone_no, :last_name);
EOL;
$stm = $conn->prepare($query);
$success = $stm->execute([
'email' => $this->email,
'first_name' => $this->first_name,
'password' => $this->password,
'phone_no' => $this->phone_no,
'last_name' => $this->last_name
]);

if ($inserted_id === null) {
if (!$success) {
$conn->rollBack();
$conn = null;
return false;
}

$this->user_id = $inserted_id;
$this->user_id = (int)$conn->lastInsertId();

// get data to be inserted to client table
$client_data = [
// perform insertion to client table
$query = <<< EOL
INSERT INTO client(user_id, street, city, district_id)
VALUES(:user_id, :street, :city, :district_id);
EOL;
$stm = $conn->prepare($query);
$success = $stm->execute([
'user_id' => $this->user_id,
'street' => $this->address->getStreet(),
'city' => $this->address->getCity(),
'district_id' => $this->address->getDistrictID()
];
]);

// perform insertion to client table
$this->insert($client_data, $this->table);
if (!$success) {
$conn->rollBack();
$conn = null;
return false;
}

return true; // insertion was successful
$conn->commit();
$conn = null;
return true;
}

public function updateUser(bool $updatePassword = false): bool
Expand Down
4 changes: 0 additions & 4 deletions src/views/404.php

This file was deleted.

Loading

0 comments on commit 4834d5a

Please sign in to comment.