$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"; $results = $this->db->query($query, [ ':id' => $id ]); if (!is_array($results) || empty($results)) { throw new EntityNotFoundException("Unable to find User"); } 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"; $results = $this->db->query($query, [ ':email' => $email ]); if (!is_array($results) || empty($results)) { throw new EntityNotFoundException("Unable to find User"); } return $this->userFromQueryResults($results); } public function create(User $user): bool { $results = $this->db->execute(self::QUERY_CREATE, [ ':email' => $user->getEmail(), ':password' => $this->userAuth->hash($user->getPassword()), ':name' => $user->getName(), ]); if ($results) { $insertId = $this->db->getConnection()->lastInsertId(); $user->setId($insertId); $user->setCreatedAt(new DateTimeImmutable()); $user->setUpdatedAt(new DateTime()); } return $results; } public function update(User $user): bool { $results = $this->db->execute(self::QUERY_UPDATE, [ ':email' => $user->getEmail(), ':name' => $user->getName(), ':password' => $this->userAuth->hash($user->getPassword()), ':id' => $user->getId() ]); if ($results == true) { $user->setUpdatedAt(new DateTime()); } return $results; } /** * @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; } } }