You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/Core/Gateway/AccountGateway.php

121 lines
3.7 KiB

<?php
namespace IQBall\Core\Gateway;
use IQBall\Core\Connection;
use IQBall\Core\Data\Account;
use IQBall\Core\Data\User;
use PDO;
class AccountGateway {
private Connection $con;
/**
* @param Connection $con
*/
public function __construct(Connection $con) {
$this->con = $con;
}
public function insertAccount(string $name, string $email, string $token, string $hash, string $profilePicture): int {
$this->con->exec("INSERT INTO Account(username, hash, email, token,profilePicture) VALUES (:username,:hash,:email,:token,:profilePic)", [
':username' => [$name, PDO::PARAM_STR],
':hash' => [$hash, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR],
':token' => [$token, PDO::PARAM_STR],
':profilePic' => [$profilePicture, PDO::PARAM_STR],
]);
return intval($this->con->lastInsertId());
}
/**
* @param string $email
* @return array<string, mixed>|null
*/
private function getRowsFromMail(string $email): ?array {
return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null;
}
/**
* @param string $email
* @return string|null the hashed user's password, or null if the given mail does not exist
*/
public function getHash(string $email): ?string {
$results = $this->getRowsFromMail($email);
if ($results == null) {
return null;
}
return $results['hash'];
}
/**
* @param string $email
* @return bool true if the given email exists in the database
*/
public function exists(string $email): bool {
return $this->getRowsFromMail($email) != null;
}
/**
* @param string $email
* @return Account|null
*/
public function getAccountFromMail(string $email): ?Account {
$acc = $this->getRowsFromMail($email);
if (empty($acc)) {
return null;
}
return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"]));
}
/**
* @param string $token get an account from given token
* @return Account|null
*/
public function getAccountFromToken(string $token): ?Account {
$acc = $this->con->fetch("SELECT * FROM Account WHERE token = :token", [':token' => [$token, PDO::PARAM_STR]])[0] ?? null;
if (empty($acc)) {
return null;
}
return new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profilePicture"]));
}
public function updateProfile(string $name, string $email, int $id) : void {
$this->con->exec("
UPDATE Account
SET email = :email AND username = :username
WHERE id = :id
", [
':username' => [$name, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR],
':id' => [$id, PDO::PARAM_INT]
]);
}
public function nameIsDifferent(string $email, string $name) : bool {
$nameExist = $this->con->fetch("SELECT username FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null;
if (!empty($nameExist)) {
if ($name != $nameExist[0]["username"]) {
return true;
}
}
return false;
}
public function changeName(string $email, string $newName) {
error_log($email);
$this->con->exec("
UPDATE Account
SET username = :username
WHERE email = :email
", [
':username' => [$newName, PDO::PARAM_STR],
':email' => [$email, PDO::PARAM_STR]
]);
}
}