From e2a3c95f99688b74192224bbd93b50c9e0d51019 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 23 May 2024 21:33:33 -0400 Subject: [PATCH] Flesh out some of the User repository methods. --- app/src/Domain/Repository/UserRepository.php | 29 ++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/app/src/Domain/Repository/UserRepository.php b/app/src/Domain/Repository/UserRepository.php index a2cd438..704a6bd 100644 --- a/app/src/Domain/Repository/UserRepository.php +++ b/app/src/Domain/Repository/UserRepository.php @@ -3,6 +3,7 @@ namespace Slovocast\Domain\Repository; use Slovocast\Infrastructure\DatabaseConnectionInterface; +use Slovocast\Infrastructure\User\UserAuthorizationInterface; use Slovocast\Exception\EntityNotFoundException; use Slovocast\Domain\Entity\User; use PDO; @@ -10,7 +11,8 @@ use PDO; class UserRepository implements UserRepositoryInterface { public function __construct( - private DatabaseConnectionInterface $database + private DatabaseConnectionInterface $database, + private UserAuthorizationInterface $userAuth ) {} /** @@ -56,11 +58,10 @@ class UserRepository implements UserRepositoryInterface public function get(int $id): User { $query = "SELECT * FROM users WHERE id = :id LIMIT 1"; - $connection = $this->database->getConnection(); $userData = $this->queryForUser( $query, [ 'id' => $id ], - $connection + $this->database->getConnection() ); return $this->userFromQueryResults($userData); } @@ -68,12 +69,30 @@ class UserRepository implements UserRepositoryInterface public function getFromEmail(string $email): User { $query = "SELECT * FROM users WHERE email = :email LIMIT 1"; - $connection = $this->database->getConnection(); $userData = $this->queryForUser( $query, [ 'email' => $email ], - $connection + $this->database->getConnection() ); return $this->userFromQueryResults($userData); } + + public function save(User $user): bool + { + $connection = $this->database->getConnection(); + $query = "INSERT INTO users (email, password, name) + VALUES (:email, :password, :name)"; + + $statement = $connection->prepare($query); + return $statement->execute([ + 'email' => $user->getEmail(), + 'name'=> $user->getName(), + 'password' => $this->userAuth->hash($user->getPassword()) + ]); + } + + public function update(User $user): bool + { + + } }