Skip to content

Commit

Permalink
create a utility class to store all global utilities
Browse files Browse the repository at this point in the history
create a utility class to store all global utilities

use utility class
  • Loading branch information
creme332 committed Jan 20, 2024
1 parent 44fb523 commit a34b82f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/controllers/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Steamy\Controller;

use Steamy\Core\Controller;
use Steamy\Core\Utility;
use Steamy\Model\User;

class Dashboard
Expand All @@ -14,7 +15,7 @@ public function index(): void
// if user is unauthenticated, redirect to login page
session_regenerate_id();
if (!array_key_exists('user', $_SESSION) || !isset($_SESSION['user'])) {
redirect('login');
Utility::redirect('login');
}

$css_file = ROOT . "/styles/views/Dashboard.css";
Expand Down
6 changes: 2 additions & 4 deletions src/controllers/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Steamy\Controller;

use Steamy\Core\Controller;
use Steamy\Core\Utility;
use Steamy\Model\User;

class Login
Expand All @@ -16,8 +17,6 @@ private function validateUser($user): bool
if ($user->validate($_POST)) {
$user_profile = $user->where($_POST);

show($user_profile);

if (empty($user_profile)) {
$this->data['errors']['other'] = 'Account does not exist';
return false;
Expand All @@ -32,7 +31,6 @@ private function validateUser($user): bool
public function index(): void
{
$user = new User();
$css_file = ROOT . "/styles/views/Login.css";

$this->data['defaultName'] = $user->getName();
$this->data['defaultPassword'] = $user->getPassword();
Expand All @@ -52,7 +50,7 @@ public function index(): void
// user details are correct
// setup session and redirect to dashboard
$_SESSION['user'] = $user->getName();
redirect('dashboard');
Utility::redirect('dashboard');
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/controllers/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Steamy\Controller;

use Steamy\Core\Controller;
use Steamy\Core\Utility;
use Steamy\Model\User;

class Profile
Expand All @@ -16,13 +17,13 @@ public function index(): void
// if user is not signed in, redirect to login page
session_regenerate_id();
if (!array_key_exists('user', $_SESSION) || !isset($_SESSION['user'])) {
redirect('login');
Utility::redirect('login');
}

// log out user if logout button clicked
if (isset($_POST['logout_submit'])) {
$_SESSION = array();
redirect('login');
Utility::redirect('login');
}

// fetch user details from database
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Steamy\Controller;

use Steamy\Core\Controller;
use Steamy\Core\Utility;
use Steamy\Model\User;

class Register
Expand All @@ -28,7 +29,7 @@ public function index(): void
unset($_POST['register_submit']);
unset($_POST['confirmPassword']);
$user->insert($_POST);
redirect('login');
Utility::redirect('login');
}
$data['errors'] = $user->errors;
}
Expand Down
7 changes: 1 addition & 6 deletions src/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ class App
// manage routes and methods
// $req = $_SERVER['REQUEST_URI'];
//$method = $_SERVER['REQUEST_METHOD'];
private function splitURL(): array
{
$URL = $_GET['url'] ?? 'home';
return explode("/", trim($URL, '/'));
}

/**
* Calls appropriate controller class to deal with URL.
* @return void
*/
public function loadController(): void
{
$URL = $this->splitURL();
$URL = Utility::splitURL();

$controllerClassName = 'Steamy\\Controller\\' . ucfirst($URL[0]);

Expand Down
45 changes: 45 additions & 0 deletions src/core/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Steamy\Core;

class Utility
{
/**
* Display any data in a formatted block. Use this function
* for debugging.
* @param $stuff
* @return void
*/
public static function show($stuff): void
{
echo "<pre>";
print_r($stuff);
echo "</pre>";
}

/**
* Splits the URL into an array of segments.
*
* This function retrieves the 'url' parameter from the $_GET array or defaults to 'home',
* trims leading and trailing slashes, and then explodes the URL into an array of segments.
*
* @return array An array containing the URL segments.
*/
public static function splitURL(): array
{
$URL = $_GET['url'] ?? 'home';
return explode("/", trim($URL, '/'));
}

/** @noinspection PhpNoReturnAttributeCanBeAddedInspection */
/**
* Redirects website to a page.
* @param $path string relative URL of page
* @return void
*/
public static function redirect(string $path): void
{
header("Location: " . ROOT . "/" . $path);
die;
}
}
26 changes: 0 additions & 26 deletions src/core/functions.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
session_start(); // start a new session

require '../vendor/autoload.php'; // autoload classes
require 'config.php'; // define configurations for database and root path
require 'functions.php'; // define global helper functions
require 'config.php'; // define configurations for database and root path

0 comments on commit a34b82f

Please sign in to comment.