. */ namespace Sikofitt\App\Repository; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Query; use Sikofitt\App\Entity\User; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Validation; /** * Class UserRepository. * * Doctrine repository for User entity. */ class UserRepository extends EntityRepository { public function findByEmail(string $email) { return $this->findBy(['email' => $email]); /*return $this->createQueryBuilder('u') ->select('u') ->where('u.email = :email') ->setParameter('email', $email) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_OBJECT);*/ } public function getKatrinaCount() { return $this->createQueryBuilder('u') ->select('count(u.familySide)') ->distinct(true) ->where('u.familySide = :side') ->setParameter('side', User::KATRINA_SIDE) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); } public function getEricCount() { return $this->createQueryBuilder('u') ->select('count(u.familySide)') ->distinct(true) ->where('u.familySide = :side') ->setParameter('side', User::ERIC_SIDE) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); } public function getEmail(string $email) { $validator = Validation::createValidator(); $emailConstraint = new Email([ 'checkMX' => true, 'checkHost' => true, 'strict' => true, ]); $result = $validator->validate($email, [$emailConstraint]); if ($result->count() > 0) { return $result; } return $this->createQueryBuilder('u') ->select('u.email') ->where('u.email = :email') ->setParameter('email', $email) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); } public function getUserByToken(string $token) { return $this->createQueryBuilder('u') ->select(['u']) ->where('u.token = :token') ->setParameter('token', $token) ->getQuery() ->getOneOrNullResult(); } public function setResetToken(string $email) { $bytes = \ParagonIE_Sodium_Compat::randombytes_buf(22); $blake = \ParagonIE_Sodium_Compat::crypto_generichash($bytes); $token = \ParagonIE_Sodium_Core_BLAKE2b::bin2hex($blake); return (bool) $this->createQueryBuilder('u') ->update() ->set('u.token', ':token') ->setParameter('token', $token) ->where('u.email = :email') ->setParameter('email', $email) ->getQuery() ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); } public function updatePassword(UserInterface $user) { $user->setToken(null); $this->getEntityManager()->persist($user); $this->getEntityManager()->flush(); } }