src/Repository/UserRepository.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  7. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method User[]    findAll()
  13.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  16. {
  17.     public function __construct(ManagerRegistry $registry)
  18.     {
  19.         parent::__construct($registryUser::class);
  20.     }
  21.     /**
  22.      * Used to upgrade (rehash) the user's password automatically over time.
  23.      */
  24.     public function upgradePassword(UserInterface $userstring $newEncodedPassword): void
  25.     {
  26.         if (!$user instanceof User) {
  27.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'\get_class($user)));
  28.         }
  29.         $user->setPassword($newEncodedPassword);
  30.         $this->_em->persist($user);
  31.         $this->_em->flush();
  32.     }
  33.     public function findByRole(string $role)
  34.     {
  35.         $roleQuery "'%".$role."%'";
  36.         return $this
  37.             ->createQueryBuilder('qb')
  38.             ->where('qb.roles LIKE '.$roleQuery)
  39.             ->getQuery()
  40.             ->getResult()
  41.         ;
  42.     }
  43.     public function findActiveUsersByRole(string $role)
  44.     {
  45.         $roleQuery "'%".$role."%'";
  46.         return $this
  47.             ->createQueryBuilder('qb')
  48.             ->where('qb.roles LIKE '.$roleQuery)
  49.             ->andWhere('qb.status = 1')
  50.             ->getQuery()
  51.             ->getResult()
  52.         ;
  53.     }
  54.     public function findAllBySearchParams(array $searchParams, array $paginationParams)
  55.     {
  56.         $results = [];
  57.         $qb $this->createQueryBuilder('qb');
  58.         // User Id filter
  59.         if (array_key_exists('userId'$searchParams)) {
  60.             $qb->andWhere('qb.id = '.$searchParams['userId']);
  61.         }
  62.         // Name filter
  63.         if (array_key_exists('term'$searchParams)) {
  64.             $termQuery "'%".$searchParams['term']."%'";
  65.             $qb
  66.                 ->andWhere('qb.firstname LIKE '.$termQuery)
  67.                 ->orWhere('qb.lastname LIKE '.$termQuery)
  68.             ;
  69.         }
  70.         // Roles[] filter
  71.         if (array_key_exists('roles'$searchParams)) {
  72.             foreach ($searchParams['roles'] as $key => $role) {
  73.                 $roleQuery "'%".$role."%'";
  74.                 if (=== $key) {
  75.                     $qb->andWhere('qb.roles LIKE '.$roleQuery);
  76.                 } else {
  77.                     $qb->orWhere('qb.roles LIKE '.$roleQuery);
  78.                 }
  79.             }
  80.         }
  81.         // Status[] filter
  82.         if (array_key_exists('status'$searchParams)) {
  83.             foreach ($searchParams['status'] as $key => $status) {
  84.                 if (=== $key) {
  85.                     $qb->andWhere('qb.status = '.$status);
  86.                 } else {
  87.                     $qb->orWhere('qb.status = '.$status);
  88.                 }
  89.             }
  90.         }
  91.         $pageCount count($qb->getQuery()->getScalarResult()) / $paginationParams['perPage'];
  92.         $results['pageCount'] = $pageCount > (int) $pageCount round((int) $pageCount 10PHP_ROUND_HALF_DOWN) : ($pageCount);
  93.         $qb
  94.             ->addOrderBy('qb.status''DESC')
  95.             ->addOrderBy('qb.lastname''ASC')
  96.             ->setFirstResult($paginationParams['offset'])
  97.             ->setMaxResults($paginationParams['perPage'])
  98.         ;
  99.         $results['data'] = $qb->getQuery()->getResult();
  100.         return $results;
  101.     }
  102. }