src/Domain/Repository/UserRepository.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace App\Domain\Repository;
  4. use App\Domain\Entity\User;
  5. use App\Domain\Repository\Interfaces\UserRepositoryInterface;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\NonUniqueResultException;
  8. use Doctrine\ORM\OptimisticLockException;
  9. use Doctrine\ORM\ORMException;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
  12. class UserRepository extends ServiceEntityRepository implements UserLoaderInterfaceUserRepositoryInterface
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryUser::class);
  17.     }
  18.     /**
  19.      * @param string $username
  20.      *
  21.      * @return User|null
  22.      *
  23.      * @throws NonUniqueResultException
  24.      */
  25.     public function getUserByUsername(string $username): ?User
  26.     {
  27.         return $this->createQueryBuilder('user')
  28.             ->where('user.username = :username')
  29.             ->setParameter('username'$username)
  30.             ->setCacheable(true)
  31.             ->getQuery()
  32.             ->getOneOrNullResult();
  33.     }
  34.     /**
  35.      * @param string $mail
  36.      *
  37.      * @return User|null
  38.      *
  39.      * @throws NonUniqueResultException
  40.      */
  41.     public function getUserByEmail($mail): ?User
  42.     {
  43.         return $this->createQueryBuilder('user')
  44.             ->where('user.email = :email')
  45.             ->setParameter('email'$mail)
  46.             ->setCacheable(true)
  47.             ->getQuery()
  48.             ->getOneOrNullResult();
  49.     }
  50.     /**
  51.      * @param string $username
  52.      * @param string $mail
  53.      *
  54.      * @return mixed
  55.      *
  56.      * @throws NonUniqueResultException
  57.      */
  58.     public function getUserByUsernameAndEmail(string $usernamestring $mail)
  59.     {
  60.         return $this->createQueryBuilder('user')
  61.             ->where('user.username = :username AND user.email = :email')
  62.             ->setParameter('username'$username)
  63.             ->setParameter('email'$mail)
  64.             ->getQuery()
  65.             ->getOneOrNullResult();
  66.     }
  67.     /**
  68.      * @return int|mixed|string
  69.      */
  70.     public function getAllUser()
  71.     {
  72.         return $this->createQueryBuilder('u')
  73.             ->orderBy('u.createdAt''DESC')
  74.             ->getQuery()
  75.             ->getResult()
  76.             ;
  77.     }
  78.     /**
  79.      * @param $role
  80.      * @return mixed
  81.      */
  82.     public function getAllUserByRoles($role)
  83.     {
  84.         return $this->createQueryBuilder('user')
  85.             ->where('user.role LIKE :role')
  86.             ->setParameter('role''%"'.$role.'"%')
  87.             ->orderBy('user.createdAt''DESC')
  88.             ->getQuery()
  89.             ->getResult()
  90.             ;
  91.     }
  92.     /**
  93.      * @param string $token
  94.      *
  95.      * @return mixed
  96.      *
  97.      * @throws NonUniqueResultException
  98.      */
  99.     public function getUserByToken(string $token)
  100.     {
  101.         return $this->createQueryBuilder('user')
  102.             ->where('user.token = :token')
  103.             ->setParameter('token'$token)
  104.             ->getQuery()
  105.             ->getOneOrNullResult();
  106.     }
  107.     /**
  108.      * @param string $id
  109.      *
  110.      * @return mixed
  111.      *
  112.      * @throws NonUniqueResultException
  113.      */
  114.     public function getUserById(string $id): ?User
  115.     {
  116.         return$this->createQueryBuilder('u')
  117.             ->where('u.id = :id')
  118.             ->setParameter('id'$id)
  119.             ->setCacheable(true)
  120.             ->getQuery()
  121.             ->getOneOrNullResult()
  122.             ;
  123.     }
  124.     /**
  125.      * @param string $username
  126.      *
  127.      * @return mixed
  128.      *
  129.      * @throws NonUniqueResultException
  130.      */
  131.     public function loadUserByUsername(string $username)
  132.     {
  133.         return $this->createQueryBuilder('user')
  134.             ->where('user.username = :username')
  135.             ->setParameter('username'$username)
  136.             ->setCacheable(true)
  137.             ->getQuery()
  138.             ->getOneOrNullResult();
  139.     }
  140.     /**
  141.      * @param string $id
  142.      * @param bool $value
  143.      * @return \Doctrine\ORM\Query
  144.      */
  145.     public function disableUserById(string $idbool $value)
  146.     {
  147.         return $this->createQueryBuilder('user')
  148.             ->update('App:User''u')
  149.             ->set('u.status''?1')
  150.             ->where('u.id = ?2')
  151.             ->setParameter(1$value)
  152.             ->setParameter(2$id)
  153.             ->getQuery()
  154.             ->execute();
  155.     }
  156.     /**
  157.      * @param $user
  158.      *
  159.      * @throws ORMException
  160.      *
  161.      * @throws OptimisticLockException
  162.      */
  163.     public function save($user)
  164.     {
  165.         $this->getEntityManager()->persist($user);
  166.         $this->getEntityManager()->flush();
  167.     }
  168.     /**
  169.      * @throws ORMException
  170.      * @throws OptimisticLockException
  171.      */
  172.     public function flush()
  173.     {
  174.         $this->getEntityManager()->flush();
  175.     }
  176.     /**
  177.      * @return void
  178.      *
  179.      * @throws ORMException
  180.      * @throws OptimisticLockException
  181.      */
  182.     public function update(): void
  183.     {
  184.         $this->getEntityManager()->flush();
  185.     }
  186.     /**
  187.      * @param $user
  188.      * @return void
  189.      *
  190.      * @throws ORMException
  191.      * @throws OptimisticLockException
  192.      */
  193.     public function updateUser($user): void
  194.     {
  195.         $this->getEntityManager()->flush();
  196.     }
  197.     /**
  198.      * @param string $id
  199.      *
  200.      * @throws NonUniqueResultException
  201.      * @throws ORMException
  202.      * @throws OptimisticLockException
  203.      */
  204.     public function deleteUser(string $id)
  205.     {
  206.         $user $this->getUserById($id);
  207.         $this->getEntityManager()->remove($user);
  208.         $this->getEntityManager()->flush();
  209.     }
  210. }