gateway->insertUser($username, $email, $passwd, false, 1); } /** * Deletes a user by their ID. * * @param string $id The ID of the user to be deleted. * * @return bool Returns true if the user was successfully deleted, false otherwise. */ public function deleteUser(string $id) : bool { // Calls the gateway method to delete the user by their ID. return $this->gateway->delete($id); } /** * Retrieves the total number of users in the system. * * @return int The total number of users. */ public function getNumberOfUsers() : int { // Calls the gateway method to get the number of users and returns the count. return $this->gateway->getNumberOfUsers()[0]['count'] ?? 0; } /** * Retrieves a user by their username. * * @param string $username The username of the user to retrieve. * * @return UserEntity|null Returns a UserEntity object if the user is found, or null if not. */ public function getUsername(string $username) : ?UserEntity { // Fetches the user details using the gateway method findUsername. $res = $this->gateway->findUsername($username); // If the user is found, return a UserEntity object with the user's details. if ($res) return new UserEntity( $res[0]['id_user'], $res[0]['username'], $res[0]['password'], $res[0]['email'], $res[0]['imgpath'], $res[0]['creation'] ); // Return null if no user is found. return null; } /** * Retrieves a user by their email. * * @param string $email The email of the user to retrieve. * * @return UserEntity|null Returns a UserEntity object if the user is found, or null if not. */ public function getEmail(string $email) : ?UserEntity { // Fetches the user details using the gateway method findEmail. $res = $this->gateway->findEmail($email); // If the user is found, return a UserEntity object with the user's details. if ($res) return new UserEntity( $res[0]['id_user'], $res[0]['username'], $res[0]['password'], $res[0]['email'], $res[0]['img'], $res[0]['creation'] ); // Return null if no user is found. return null; } /** * Retrieves the user ID based on the username. * * @param string $username The username of the user. * * @return int The user ID. */ public function getIdByUsername(string $username) { // Fetches the user ID using the gateway method getIdUser. $res = $this->gateway->getIdUser($username); return $res[0]['id_user']; } /** * Checks if a given username already exists. * * @param string $username The username to check. * * @return bool Returns true if the username exists, false otherwise. */ public function IsExisteUsername(string $username) : bool { // Checks if the username exists using the gateway method. return $this->gateway->IsExisteUsername($username); } /** * Checks if a given email already exists. * * @param string $email The email to check. * * @return bool Returns true if the email exists, false otherwise. */ public function IsExisteEmail(string $email) : bool { // Checks if the email exists using the gateway method. return $this->gateway->IsExisteEmail($email); } /** * Verifies if the provided password matches the stored password hash for the given username. * * @param string $username The username for the user. * @param string $passWd The password to verify. * * @return bool Returns true if the password is correct, false otherwise. */ public function isPassWd(string $username, string $passWd) : bool { // Fetches the stored password hash for the username. $hash = $this->gateway->getPasswordHash($username); // Verifies the password using password_verify. return $hash !== null && password_verify($passWd, $hash); } /** * Checks if a quote is marked as a favorite by the current user. * * @param string|null $username The username of the user (can be null). * @param int $idq The ID of the quote to check. * * @return bool Returns true if the quote is a favorite, false otherwise. */ public function isFavorite(?string $username, int $idq) : bool { // If the user is not logged in (session is null), return false. if ($_SESSION["user"] == NULL) { return false; } else { // Checks if the quote is in the user's favorites. $res = $this->gateway->inFavorite($_SESSION["user"], $idq); return $res; } } /** * Updates the username for a user. * * @param string $username The current username. * @param string $newUsername The new username to set. * * @return string Returns the new username if it was successfully updated, or the current username if the new one already exists. */ public function setUsername(string $username, string $newUsername) : string { // If the new username already exists, return the current username. if ($this->IsExisteUsername($newUsername)) { return $username; } // Updates the username using the gateway method. $res = $this->gateway->updateUsername($username, $newUsername); // If the update was successful, return the new username. if (!empty($res) && isset($res[0]['username'])) { return $res[0]['username']; } // Return the original username if the update was unsuccessful. return $username; } /** * Updates the email for a user. * * @param string $username The current username. * @param string $newEmail The new email to set. * * @return string Returns the new email if it was successfully updated, or the current username if the new email already exists. */ public function setEmail(string $username, string $newEmail) { // If the new email already exists, return the current username. if ($this->IsExisteEmail($newEmail)) { return $username; } // Updates the email using the gateway method. $res = $this->gateway->updateEmail($username, $newEmail); // If the update was successful, return the new email. if (!empty($res) && isset($res[0]['email'])) { return $res[0]['email']; } // Return the original username if the update was unsuccessful. return $username; } /** * Updates the profile image for a user. * * @param string $username The username of the user. * @param string $newImage The new image path to set. * * @return array Returns an array with the new image path. */ public function setImage(string $username, string $newImage) { // Updates the image path using the gateway method. $res = $this->gateway->updateImg($username, $newImage); // Returns the updated image path. $src[] = $res[0]['img']; return $src; } /** * Updates the password for a user. * * @param string $username The username of the user. * @param string $newPassWd The new password to set. */ public function setPassWd(string $username, string $newPassWd) : void { // Updates the password using the gateway method. $res = $this->gateway->updatePasswd($username, $newPassWd); } /** * Adds a quote to the user's favorites. * * @param string $username The username of the user. * @param int $id The ID of the quote to add. */ public function addFavorite(string $username, int $id) { // Adds the quote to the user's favorites. $this->gateway->addFavorite($username, $id); } /** * Removes a quote from the user's favorites. * * @param string $username The username of the user. * @param int $id The ID of the quote to remove. */ public function supFavorite(string $username, int $id) { // Removes the quote from the user's favorites. $this->gateway->supFavorite($username, $id); } /** * Deletes all comments made by the specified user. * * @param string $username The username of the user. */ public function deleteAllCommentary(string $username) { // Deletes all comments made by the user. $this->gateway->deleteAllCommentaryUser($username); } /** * Deletes all favorite quotes of the specified user. * * @param string $username The username of the user. */ public function deleteAllFavorite(string $username) { // Deletes all favorite quotes of the user. $this->gateway->deleteAllFavoriteUser($username); } /** * Deletes the user's account. * * @param string $username The username of the user to delete. */ public function deleteAccount(string $username) { // Deletes the user's account. $this->gateway->deleteUser($username); } } ?>