Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> BAD IDEA!
  • Loading branch information
NaysKutzu committed Jul 9, 2024
1 parent b5237a9 commit 0f9ce25
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 173 deletions.
18 changes: 18 additions & 0 deletions app/Database/Exceptions/DatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MythicalSystemsFramework\Database\Exceptions;

class DatabaseException extends \Exception
{
public function __construct($message, $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
49 changes: 29 additions & 20 deletions app/Database/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MythicalSystemsFramework\Database;

use Exception;
use MythicalSystemsFramework\Database\Exceptions\DatabaseException;
use MythicalSystemsFramework\Managers\ConfigManager as cfg;
use MythicalSystemsFramework\Kernel\Logger;
use MythicalSystemsFramework\Kernel\LoggerLevels;
Expand All @@ -18,44 +20,51 @@ class MySQL
* Connects to the database server using PDO.
*
* @return PDO The PDO object representing the database connection.
* @throws PDOException If the connection to the database fails.
* @throws DatabaseException If the connection to the database fails.
*/
public function connectPDO(): PDO
{

$dsn = "mysql:host=" . cfg::get("database", "host") . ";dbname=" . cfg::get("database", "name") . ";port=" . cfg::get("database", "port") . ";charset=utf8mb4";
try {
$pdo = new PDO($dsn, cfg::get("database", "username"), cfg::get("database", "password"));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to connect to the database!");
throw new PDOException("Failed to connect to the database: " . $e->getMessage());
throw new DatabaseException("Failed to connect to the database: " . $e->getMessage());
}
}

/**
* Connects to the database server using MYSQLI.
*
* @return mysqli
*
* @throws DatabaseException
*/
public function connectMYSQLI(): mysqli
{
if (!isset(self::$connection)) {
self::$connection = new mysqli(
cfg::get("database", "host"),
cfg::get("database", "username"),
cfg::get("database", "password"),
cfg::get("database", "name"),
cfg::get("database", "port")
);
try {
if (!isset(self::$connection)) {
self::$connection = new mysqli(
cfg::get("database", "host"),
cfg::get("database", "username"),
cfg::get("database", "password"),
cfg::get("database", "name"),
cfg::get("database", "port")
);

if (self::$connection->connect_error) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to connect to the database!");
if (self::$connection->connect_error) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to connect to the database!");
throw new DatabaseException("Failed to connect to the database: " . self::$connection->connect_error);
}
}
}

return self::$connection;
return self::$connection;
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to connect to the database: " . $e);
throw new DatabaseException("Failed to connect to the database: " . $e->getMessage());
}
}

/**
Expand All @@ -81,7 +90,7 @@ public static function closeConnection(): void
* @param string $database The database name
*
* @return bool True if the connection is successful, false otherwise.
* @throws PDOException If the connection to the database fails. *
* @throws DatabaseException If the connection to the database fails. *
*/
public function tryConnection(string $host, string|int $port, string $username, string $password, string $database): bool
{
Expand All @@ -90,9 +99,9 @@ public function tryConnection(string $host, string|int $port, string $username,
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
} catch (PDOException $e) {
} catch (Exception $e) {
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, "Failed to execute PDO query: " . $e);
return false;
throw new DatabaseException("Failed to execute PDO query: " . $e->getMessage());
}
}
}
}
18 changes: 18 additions & 0 deletions app/User/Exceptions/UserAccountInvalidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

class UserAccountInvalidException extends \Exception
{
public function __construct($message, $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use Throwable;

class UserException extends Exception
class UserBannedException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
Expand All @@ -18,4 +18,4 @@ public function __toString(): string {
public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
}
21 changes: 21 additions & 0 deletions app/User/Exceptions/UserDeletedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserDeletedException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
16 changes: 16 additions & 0 deletions app/User/Exceptions/UserEmailExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;
use Exception;
use Throwable;
class UserEmailExistsException extends Exception {
public function __construct($message, $code = 0, Throwable $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
21 changes: 21 additions & 0 deletions app/User/Exceptions/UserExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserExistsException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
21 changes: 21 additions & 0 deletions app/User/Exceptions/UserNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserNotFoundException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString() : string {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
23 changes: 23 additions & 0 deletions app/User/Exceptions/UserNotVerifiedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserNotVerifiedException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
23 changes: 23 additions & 0 deletions app/User/Exceptions/UserPasswordWrongException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserPasswordWrongException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
23 changes: 23 additions & 0 deletions app/User/Exceptions/UserRequestedDataNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace MythicalSystemsFramework\User\Exceptions;

use Exception;
use Throwable;

class UserRequestedDataNotFoundException extends Exception
{
public function __construct($message, $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

public function toString(): string
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
Loading

0 comments on commit 0f9ce25

Please sign in to comment.