src/Repository/UserRepository.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Agency;
  4. use App\Entity\Estimate;
  5. use App\Entity\User;
  6. use DateTime;
  7. use DateTimeInterface;
  8. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use InvalidArgumentException;
  11. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  14. use function get_class;
  15. /**
  16.  * @extends ServiceEntityRepository<User>
  17.  *
  18.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  19.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  20.  * @method User[]    findAll()
  21.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  22.  */
  23. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  24. {
  25.     public function __construct (ManagerRegistry $registry)
  26.     {
  27.         parent::__construct($registryUser::class);
  28.     }
  29.     /**
  30.      * @param User $entity
  31.      * @param bool $flush
  32.      */
  33.     public function add (User $entitybool $flush true): void
  34.     {
  35.         $this->_em->persist($entity);
  36.         if ($flush) {
  37.             $this->_em->flush();
  38.         }
  39.     }
  40.     /**
  41.      * @param Agency[] $ids
  42.      * @return User[]
  43.      */
  44.     public function findByAgencies (array $ids): array
  45.     {
  46.         $qb $this->createQueryBuilder('u');
  47.         $qb
  48.             ->distinct()
  49.             ->join('u.agencies''a')
  50.             ->andWhere($qb->expr()->in('a.id'$ids))
  51.             ->andWhere($qb->expr()->eq('u.active'true));
  52.         return $qb->getQuery()->getResult();
  53.     }
  54.     /**
  55.      * @param string | int $id
  56.      * @return User[]
  57.      */
  58.     public function findByAgency ($id): array
  59.     {
  60.         $qb $this->createQueryBuilder('u');
  61.         $qb
  62.             ->distinct()
  63.             ->join('u.agencies''a')
  64.             ->andWhere($qb->expr()->eq('a.id'$id));
  65.         return $qb->getQuery()->getResult();
  66.     }
  67.     /**
  68.      * @param string $role
  69.      * @return User[]
  70.      */
  71.     public function findByRole (string $role): array
  72.     {
  73.         if (!array_key_exists($roleUser::getRolesList())) return [];
  74.         $qb $this->createQueryBuilder('u');
  75.         $qb->andWhere($qb->expr()->like('u.roles'"'%" $role "%'"));
  76.         return $qb->getQuery()->getResult();
  77.     }
  78.     /**
  79.      * @param string | null $role
  80.      * @param array $agencies
  81.      * @return User[]
  82.      */
  83.     public function findByRoleAndAgencies (string $role null, array $agencies = []): array
  84.     {
  85.         $qb $this->createQueryBuilder('u');
  86.         if ($role && array_key_exists($roleUser::getRolesList())) $qb->andWhere($qb->expr()->like('u.roles'"'%" $role "%'"));
  87.         if (count($agencies)) {
  88.             $qb
  89.                 ->leftJoin("u.agencies""a")
  90.                 ->andWhere($qb->expr()->in("a.id"$agencies));
  91.         }
  92.         $qb->addOrderBy("u.firstname");
  93.         $qb->addOrderBy("u.lastname");
  94.         return $qb->getQuery()->getResult();
  95.     }
  96.     /**
  97.      * @param string | int $id
  98.      * @return User[]
  99.      */
  100.     public function findCommercialByAgency ($id): array
  101.     {
  102.         $qb $this->createQueryBuilder('u');
  103.         $qb
  104.             ->distinct()
  105.             ->join('u.agencies''a')
  106.             ->andWhere($qb->expr()->eq('a.id'$id));
  107.         return $qb->getQuery()->getResult();
  108.     }
  109.     /**
  110.      * @param array $ids
  111.      * @return User[]
  112.      */
  113.     public function findTechnicienByAgencies (array $ids): array
  114.     {
  115. //        dd($ids);
  116.         $qb $this->createQueryBuilder('u');
  117.         $qb
  118.             ->distinct()
  119.             ->andWhere($qb->expr()->eq('u.active'true))
  120.             ->join('u.agencies''a')
  121.             ->andWhere($qb->expr()->in('a.id'$ids))
  122.             ->andWhere($qb->expr()->like("u.roles"$qb->expr()->literal('%"' User::ROLE_TECHNICIEN '"%')));
  123.         return $qb->getQuery()->getResult();
  124.     }
  125.     /**
  126.      * @param User $entity
  127.      * @param bool $flush
  128.      */
  129.     public function remove (User $entitybool $flush true): void
  130.     {
  131.         $this->_em->remove($entity);
  132.         if ($flush) {
  133.             $this->_em->flush();
  134.         }
  135.     }
  136.     /**
  137.      * Used to upgrade (rehash) the user's password automatically over time.
  138.      */
  139.     public function upgradePassword (PasswordAuthenticatedUserInterface $userstring $newHashedPassword): void
  140.     {
  141.         if (!$user instanceof User) {
  142.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_class($user)));
  143.         }
  144.         $user->setPassword($newHashedPassword);
  145.         $this->_em->persist($user);
  146.         $this->_em->flush();
  147.     }
  148. }