src/Repository/UserRepository.php line 22

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\ORM\OptimisticLockException;
  6. use Doctrine\ORM\ORMException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  11. /**
  12.  * @method User|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method User|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method User[]    findAll()
  15.  * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  18. {
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registry, User::class);
  22.     }
  23.     /**
  24.      * @throws ORMException
  25.      * @throws OptimisticLockException
  26.      */
  27.     public function add(User $entity, bool $flush = true): void
  28.     {
  29.         $this->_em->persist($entity);
  30.         if ($flush) {
  31.             $this->_em->flush();
  32.         }
  33.     }
  34.     /**
  35.      * @throws ORMException
  36.      * @throws OptimisticLockException
  37.      */
  38.     public function remove(User $entity, bool $flush = true): void
  39.     {
  40.         $this->_em->remove($entity);
  41.         if ($flush) {
  42.             $this->_em->flush();
  43.         }
  44.     }
  45.     /**
  46.      * Used to upgrade (rehash) the user's password automatically over time.
  47.      */
  48.     public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
  49.     {
  50.         if (!$user instanceof User) {
  51.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  52.         }
  53.         $user->setPassword($newHashedPassword);
  54.         $this->_em->persist($user);
  55.         $this->_em->flush();
  56.     }
  57.     /**
  58.      * @return User Returns one User
  59.      */
  60.     public function findUserBy($user_id)
  61.     {
  62.         return $this->createQueryBuilder('u')
  63.             ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, c.rasion_sociale, c.adr1, c.adr2, 
  64.                         c.cp, c.cedex, c.email AS email_carrier, c.tel as tel_carrier, c.ville, rc.nicename, rc.phonecode, rc.id AS ref_country_id,
  65.                         c.siret,c.site_web,c.code_ape,c.forme,c.capital_social')
  66.             ->leftJoin('u.carriers', 'c')
  67.             ->leftJoin('c.ref_country', 'rc')
  68.             ->andWhere('u.id = :user_id')
  69.             ->setParameter('user_id', $user_id)
  70.             ->getQuery()
  71.             ->getResult()
  72.         ;
  73.     }
  74.     public function findRoleByUserId($user_id)
  75.     {
  76.         return $this->createQueryBuilder('u')
  77.             ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, rc.nicename, rc.phonecode, rc.id AS ref_country_id ')
  78.             ->leftJoin('u.ref_country', 'rc')
  79.             ->andWhere('u.id = :user_id')
  80.             ->setParameter('user_id', $user_id)
  81.             ->getQuery()
  82.             ->getResult()
  83.         ;
  84.     }
  85.     
  86.     /*
  87.     public function findOneBySomeField($value): ?User
  88.     {
  89.         return $this->createQueryBuilder('u')
  90.             ->andWhere('u.exampleField = :val')
  91.             ->setParameter('val', $value)
  92.             ->getQuery()
  93.             ->getOneOrNullResult()
  94.         ;
  95.     }
  96.     */
  97.     /**
  98.      * @return User[] Returns an array of users objects
  99.      */
  100.     public function findAllUsersStdPrinDri($carrierId)
  101.     {
  102.         $roles = ['["ROLE_STANDARD_USER"]', '["ROLE_DRIVER_USER"]', '["ROLE_PRINCIPAL_USER"]'];
  103.         return $this->createQueryBuilder('u')
  104.             ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
  105.             ->leftJoin('u.carriers', 'c')
  106.             ->leftJoin('u.ref_country', 'rc')
  107.             ->andWhere('u.roles IN (:roles)')
  108.             ->setParameter('roles', $roles)
  109.             ->andWhere('c.id LIKE :carrier_id')
  110.             ->setParameter('carrier_id', $carrierId)
  111.             ->orderBy('u.id', 'ASC')
  112.             ->getQuery()
  113.             ->getResult();
  114.     }
  115.     public function findUserByCarrierAndRole($carrierId, $role)
  116.     {
  117.         $queryBuilder = $this->createQueryBuilder('u');
  118.         if($carrierId != null){
  119.             $queryBuilder
  120.                 ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id, u.contact_mailjet')
  121.                 ->leftJoin('u.carriers', 'c');
  122.         }else{
  123.             $queryBuilder
  124.                 ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, u.password, rc.phonecode, rc.id AS ref_country_id, u.contact_mailjet');
  125.         }
  126.         $queryBuilder
  127.             ->leftJoin('u.ref_country', 'rc')
  128.             ->andWhere('u.roles LIKE :role')
  129.             ->setParameter('role', '%"'.$role.'"%');
  130.         if($carrierId != null){
  131.             $queryBuilder->andWhere('c.id LIKE :carrier_id')
  132.                 ->setParameter('carrier_id', $carrierId);
  133.         }
  134.         return $queryBuilder->getQuery()->getResult();
  135.     }
  136.     public function findUserByCarrier($carrierId)
  137.     {
  138.         return $this->createQueryBuilder('u')
  139.             ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
  140.             ->leftJoin('u.carriers', 'c')
  141.             ->leftJoin('u.ref_country', 'rc')
  142.             ->andWhere('c.id LIKE :carrier_id')
  143.             ->setParameter('carrier_id', $carrierId)
  144.             ->getQuery()
  145.             ->getResult();
  146.     }
  147.     public function findAllUsers()
  148.     {
  149.         return $this->createQueryBuilder('u')
  150.             ->select('u.id, u.email, u.roles, u.last_name, u.first_name, u.tel, u.created_at, c.rasion_sociale, c.id AS carrierId, u.password, rc.phonecode, rc.id AS ref_country_id')
  151.             ->leftJoin('u.carriers', 'c')
  152.             ->leftJoin('u.ref_country', 'rc')
  153.             ->orderBy('u.created_at', 'DESC')
  154.             ->getQuery()
  155.             ->getResult();
  156.     }
  157. }