vendor/arshdf/user-bundle-sf5/src/Entity/UserManager.php line 30

Open in your IDE?
  1. <?php
  2. namespace ARSHDF\UserBundle\Entity;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\Persistence\ObjectManager;
  5. use Doctrine\Persistence\ObjectRepository;
  6. class UserManager
  7. {
  8.     private $class;
  9.     /**
  10.      * @var ObjectManager
  11.      */
  12.     private $om;
  13.     public function __construct(EntityManagerInterface $om$class)
  14.     {
  15.         $this->om $om;
  16.         $this->class $class;
  17.     }
  18.     public function createUser()
  19.     {
  20.         $class $this->getClass();
  21.         return new $class();
  22.     }
  23.     public function completeUser($user){
  24.         $user->setEnabled(false);
  25.         $user->setDeleted(false);
  26.         $user->setToActivate(false);
  27.         $user->setCreatedAt(new \DateTime());
  28.         $user->setPassword("");
  29.     }
  30.     /**
  31.      * @return string
  32.      */
  33.     public function getClass(): string
  34.     {
  35.         if (false !== strpos($this->class':')) {
  36.             $metadata $this->om->getClassMetadata($this->class);
  37.             $this->class $metadata->getName();
  38.         }
  39.         return $this->class;
  40.     }
  41.     public function deleteUser($user)
  42.     {
  43.         $this->om->remove($user);
  44.         $this->om->flush();
  45.     }
  46.     public function findUserById($id)
  47.     {
  48.         return $this->findUserBy(['id' => $id]);
  49.     }
  50.     public function findUserByEmail($email)
  51.     {
  52.         return $this->findUserBy(['email' => strtolower($email)]);
  53.     }
  54.     public function findUserByUsername($username)
  55.     {
  56.         return $this->findUserBy(['username' => strtolower($username)]);
  57.     }
  58.     public function findUserByUsernameOrEmail($usernameOrEmail)
  59.     {
  60.         if (preg_match('/^.+@\S+\.\S+$/'$usernameOrEmail)) {
  61.             $user $this->findUserByEmail($usernameOrEmail);
  62.             if (null !== $user) {
  63.                 return $user;
  64.             }
  65.         }
  66.         return $this->findUserByUsername($usernameOrEmail);
  67.     }
  68.     public function findUserBy(array $criteria)
  69.     {
  70.         return $this->getRepository()->findOneBy($criteria);
  71.     }
  72.     public function findUsersBy(array $criteria)
  73.     {
  74.         return $this->getRepository()->findBy($criteria);
  75.     }
  76.     public function findUsers()
  77.     {
  78.         return $this->getRepository()->findAll();
  79.     }
  80.     protected function getRepository(): ObjectRepository
  81.     {
  82.         return $this->om->getRepository($this->getClass());
  83.     }
  84.     public function getUserQueryBuilder(){
  85.         return $this->om->createQueryBuilder()
  86.             ->select('u')
  87.             ->from($this->class'u');
  88.     }
  89.     public function findOneByToken(string $token)
  90.     {
  91.         return $this->findUserBy(['token' => $token]);
  92.     }
  93.     public function findByRole(string $role){
  94.         $qb $this->om->createQueryBuilder()
  95.             ->select('u')
  96.             ->from($this->class'u')
  97.             ->andWhere('u.roles LIKE :roles')
  98.             ->setParameter('roles''%"'.$role.'"%')
  99.             ->andWhere('u.enabled = 1')
  100.         ;
  101.         return $qb->getQuery()->getResult();
  102.     }
  103. }