getConnection(); $db->exec(" CREATE TABLE IF NOT EXISTS users ( email TEXT PRIMARY KEY, vorname TEXT NOT NULL, nachname TEXT NOT NULL, password TEXT NOT NULL ); "); unset($db); } catch (PDOException $e) { throw new RuntimeException("Benutzerdatenbank konnte nicht erstellt werden."); } } private function getConnection() { try { $dsn = 'sqlite:' . __DIR__ . '/../../db/users.db'; $db = new PDO($dsn, null, null); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $db; } catch (PDOException $e) { throw new RuntimeException("Verbindung zur Benutzerdatenbank fehlgeschlagen."); } } public static function getInstance() { if (self::$instance == null) { self::$instance = new DatabaseUserManager(); } return self::$instance; } public function findUser($email) { try { $db = $this->getConnection(); $sql = "SELECT * FROM users WHERE email = :email"; $command = $db->prepare($sql); $command->execute([ ":email" => $email ]); $user = $command->fetch(PDO::FETCH_ASSOC); return $user ?: null; } catch (PDOException $e) { throw new RuntimeException("Benutzer konnte nicht geladen werden."); } } public function addUser($email, $vorname, $nachname, $password) { try { $db = $this->getConnection(); $db->beginTransaction(); $sql = " INSERT INTO users (email, vorname, nachname, password) VALUES (:email, :vorname, :nachname, :password) "; $command = $db->prepare($sql); $command->execute([ ":email" => $email, ":vorname" => $vorname, ":nachname" => $nachname, ":password" => $password ]); $db->commit(); } catch (PDOException $e) { if (isset($db) && $db->inTransaction()) { $db->rollBack(); } if ($e->getCode() === "23000") { throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet."); } throw new RuntimeException("Benutzer konnte nicht gespeichert werden."); } } public function updateUser($oldEmail, $newEmail, $vorname, $nachname, $password = null) { try { $db = $this->getConnection(); if (!empty($password)) { $sql = " UPDATE users SET email = :newEmail, vorname = :vorname, nachname = :nachname, password = :password WHERE email = :oldEmail "; $params = [ ":newEmail" => $newEmail, ":vorname" => $vorname, ":nachname" => $nachname, ":password" => password_hash($password, PASSWORD_DEFAULT), ":oldEmail" => $oldEmail ]; } else { $sql = " UPDATE users SET email = :newEmail, vorname = :vorname, nachname = :nachname WHERE email = :oldEmail "; $params = [ ":newEmail" => $newEmail, ":vorname" => $vorname, ":nachname" => $nachname, ":oldEmail" => $oldEmail ]; } $command = $db->prepare($sql); $command->execute($params); return $command->rowCount() > 0; } catch (PDOException $e) { if ($e->getCode() === "23000") { throw new InvalidArgumentException("Diese E-Mail-Adresse wird bereits verwendet."); } throw new RuntimeException("Benutzer konnte nicht aktualisiert werden."); } } public function deleteUser($email) { try { $db = $this->getConnection(); $sql = "DELETE FROM users WHERE email = :email"; $command = $db->prepare($sql); $command->execute([ ":email" => $email ]); return $command->rowCount() > 0; } catch (PDOException $e) { throw new RuntimeException("Benutzer konnte nicht gelöscht werden."); } } }