databaseConnection->getConnection()->query($sql); return $statement->fetchAll(mode: PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param Int $id * * @return array|false */ public function findByID(Int $id): bool|array { $sql = " SELECT name, api_token_prefix, api_token FROM " . DatabaseConnection::TABLE_USER . " WHERE id = :id; "; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':id', var: $id); $statement->execute(); return $statement->fetch(mode: PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $prefix * * @return bool|array */ public function findByPrefix(String $prefix): bool|array { $sql = " SELECT name, api_token FROM " . DatabaseConnection::TABLE_USER . " WHERE api_token_prefix = :prefix"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':prefix', var: $prefix); $statement->execute(); return $statement->fetch(mode: PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @return array|void */ public function create(String $name = '') { $tokenPrefix = uniqid(); $result['tokenPrefix'] = $tokenPrefix; try { $key = bin2hex(random_bytes(length: 24)); $result['key'] = $key; } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; exit(1); } $token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID); $sql = " INSERT INTO " . DatabaseConnection::TABLE_USER . " (name, api_token_prefix, api_token) VALUES (:token_prefix, :token)"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':token_prefix', var: $tokenPrefix); $statement->bindParam(param: ':token', var: $token); $statement->bindParam(param: ':name', var: $name); $statement->execute(); $result['row'] = $this->databaseConnection->getConnection()->lastInsertId(); return $result; } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param $id * * @return int */ public function delete($id): int { $sql = " DELETE FROM " . DatabaseConnection::TABLE_USER . " WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: 'id', var: $id); $statement->execute(); return $statement->rowCount(); } catch (PDOException $e) { exit($e->getMessage()); } } }