$results['id'], 'email' => $results['email'], 'password' => $results['password'], 'name' => $results['name'], 'createdAt' => $results['created_at'], 'updatedAt' => $results['updated_at'] ]); } /** * Get a single instance of the User Entity. */ public function get(int $id): User { $query = "SELECT * FROM users WHERE id = :id LIMIT 1"; $statement = $this->db->getConnection()->prepare($query); $statement->execute([ ':id' => $id ]); $results = $statement->fetch(\PDO::FETCH_ASSOC); return $this->userFromQueryResults($results); } /** * Get a single instance of the User Entity by their Email address. * * @param string $email * @return User */ public function getFromEmail(string $email): User { $query = "SELECT * FROM users WHERE email = :email LIMIT 1"; $statement = $this->db->getConnection()->prepare($query); $statement->execute([ ':email' => $email ]); $results = $statement->fetch(\PDO::FETCH_ASSOC); if (!is_array($results) || count($results)) { throw new EntityNotFoundException("Unable to find User"); } return $this->userFromQueryResults($results); } public function create(User $user): User { $query = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)"; $results = $this->db->getConnection()->exec($query, [ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(), ]); $insertId = $this->db->getConnection()->lastInsertId(); $user->setId($insertId); return $user; } public function update(User $user): bool { $query = "UPDATE users SET email = :email, name = :name, password = :password WHERE id = :id"; $statement = this->db->prepare($query); return $statement->execute([ $user->getEmail(), $user->getName(), $this->userAuth->hash($user->getPassword()), $user->getId() ]); } /** * @TODO Figure out soft/hard delete logic for users */ public function delete(User $user): bool { return false; } public function verifyPassword(string $email, string $password): bool { try { $user = $this->getFromEmail($email); return $this->authUser->verify($password, $user->getPassword()); } catch (\Exception $e) { return false; } } }