src/Repository/Auth/UserRepository.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Auth;
  3. use App\Data\SearchItem;
  4. use App\Entity\Activities\Likes;
  5. use App\Entity\Auth\User;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\Exception\ORMException;
  8. use Doctrine\ORM\NonUniqueResultException;
  9. use Doctrine\ORM\OptimisticLockException;
  10. use Doctrine\ORM\QueryBuilder;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  15. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  16. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use function get_class;
  19. /**
  20.  * @extends ServiceEntityRepository<User>
  21.  *
  22.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  23.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  24.  * @method User[]    findAll()
  25.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  26.  */
  27. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  28. {
  29.     /**
  30.      * @var PaginatorInterface
  31.      */
  32.     private PaginatorInterface $paginator;
  33.     public function __construct(ManagerRegistry $registryPaginatorInterface $paginator)
  34.     {
  35.         parent::__construct($registryUser::class);
  36.         $this->paginator $paginator;
  37.     }
  38.     /**
  39.      * @param User $entity
  40.      * @param bool $flush
  41.      * @throws OptimisticLockException
  42.      * @throws ORMException|\Doctrine\ORM\ORMException
  43.      */
  44.     public function add(User $entitybool $flush false): void
  45.     {
  46.         $this->getEntityManager()->persist($entity);
  47.         if ($flush) {
  48.             $this->getEntityManager()->flush();
  49.         }
  50.     }
  51.     /**
  52.      * @param User $entity
  53.      * @param bool $flush
  54.      * @throws OptimisticLockException
  55.      * @throws ORMException|\Doctrine\ORM\ORMException
  56.      */
  57.     public function remove(User $entitybool $flush false): void
  58.     {
  59.         $this->getEntityManager()->remove($entity);
  60.         if ($flush) {
  61.             $this->getEntityManager()->flush();
  62.         }
  63.     }
  64.     /**
  65.      * Used to upgrade (rehash) the user's password automatically over time.
  66.      * @param PasswordAuthenticatedUserInterface $user
  67.      * @param string $newHashedPassword
  68.      * @throws OptimisticLockException
  69.      * @throws ORMException|\Doctrine\ORM\ORMException
  70.      */
  71.     public function upgradePassword(PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  72.     {
  73.         if (!$user instanceof User) {
  74.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_class($user)));
  75.         }
  76.         $user->setPassword($newHashedPassword);
  77.         $this->add($usertrue);
  78.     }
  79.     /**
  80.      * @param SearchItem $search
  81.      * @param Request $request
  82.      * @param bool $paginate
  83.      * @return array
  84.      */
  85.     public function findSearch(SearchItem $searchRequest $request$paginate true): array
  86.     {
  87.         $query $this->getSearchQuery($search)->getQuery();
  88.         $page $request->query->getInt('page'1);
  89.         if ($request->get('ajax')) {
  90.             $limit 6;
  91.         } else {
  92.             $limit $paginate ? ($page) : count($query->getResult());
  93.         }
  94.         $users $this->paginator->paginate($query$page$limit);
  95.         return [$users$pagecount($users)];
  96.     }
  97.     /**
  98.      * @param SearchItem $search
  99.      * @return QueryBuilder
  100.      */
  101.     private function getSearchQuery(SearchItem $search): QueryBuilder
  102.     {
  103.         $query $this->createQueryBuilder('u');
  104.         if (!empty($search->q)) {
  105.             $query $query
  106.                 ->andWhere('u.town LIKE :q OR u.firstname LIKE :q OR u.lastname LIKE :q OR u.zipCode LIKE :q OR u.address LIKE :q')
  107.                 ->setParameter('q'"%{$search->q}%");
  108.         }
  109.         if ($search->holiday) {
  110.             $query $query->andWhere('u.emergencyKeeping = true');
  111.         }
  112.         if ($search->verified) {
  113.             $query $query->andWhere('u.isConfirmed = true');
  114.         }
  115.         if ($search->nightKeeping) {
  116.             $query $query->andWhere('u.nightKeeping = true');
  117.         }
  118.         if (!empty($search->status)) {
  119.             $query $query
  120.                 ->andWhere('u.status != :status');
  121.             if ($search->status === 'home_care') {// La nounou se déplace
  122.                 $query->setParameter('status'"Je me déplace uniquement");
  123.             }
  124.             if ($search->status === 'away_care') { // La nounou va chez le parent
  125.                 $query->setParameter('status'"Je reçois uniquement");
  126.             }
  127.         }
  128.         return $query
  129.             ->andWhere('u.isAdmin = false')
  130.             ->andWhere('u.isParent = false')
  131.             ->andWhere('u.isProfileCompleted = true')
  132.             ->andWhere('u.isVerified = true')
  133.             ->andWhere('u.bannedAt IS NULL')
  134.             ->andWhere('u.deleteAt IS NULL')
  135.             ->andWhere('u.confirmationToken IS NULL')
  136.             ->orderBy('u.createdAt''DESC');
  137.     }
  138.     /**
  139.      * Récupère le prix minimum et maximum correspondant à une recherche
  140.      * @param SearchItem $search
  141.      * @return array|integer[]
  142.      */
  143.     public function findMinMax(SearchItem $search): array
  144.     {
  145.         $results $this->getSearchQuery($search)
  146.             ->select('MIN(u.hourRate) as min''MAX(u.hourRate) as max')
  147.             ->getQuery()
  148.             ->getScalarResult();
  149.         return [(float)$results[0]['min'], (float)$results[0]['max']];
  150.     }
  151.     /**
  152.      * @param User|UserInterface $user
  153.      * @return User[]
  154.      */
  155.     public function getFavorites(User $user)
  156.     {
  157.         return $this->createQueryBuilder('u')
  158.             ->innerJoin(Likes::class, 'l''WITH''l.liked = u.id')
  159.             ->innerJoin(User::class, 'liker''WITH''liker.id = l.liker')
  160.             ->andWhere('l.isActive = true')
  161.             ->andWhere('l.liker = :user')
  162.             ->setParameter('user'$user)
  163.             ->getQuery()->getResult();
  164.     }
  165.     /**
  166.      * @param string $emailOrUsername
  167.      * @return User|null
  168.      * @throws NonUniqueResultException
  169.      */
  170.     public function findByEmailOrUsername(string $emailOrUsername)
  171.     {
  172.         return $this->createQueryBuilder('u')
  173.             ->where('u.email = :emailOrUsername')
  174.             ->orWhere('u.matricule = :emailOrUsername')
  175.             ->setParameter('emailOrUsername'$emailOrUsername)
  176.             ->getQuery()
  177.             ->getOneOrNullResult();
  178.     }
  179. }