src/Repository/UserRepository.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Repository;
  4. use DateTime;
  5. use DateTimeZone;
  6. use Doctrine\Common\Persistence\ManagerRegistry;
  7. use Doctrine\ORM\OptimisticLockException;
  8. use Doctrine\ORM\ORMException;
  9. use Doctrine\ORM\Query\Expr\Join;
  10. use Harmonizely\Core\ORM\Tools\Contract\IPaginator;
  11. use Harmonizely\Core\ORM\Tools\Paginator;
  12. use Harmonizely\DTO\SuperAdmin\Customer\Request\GetCustomerListRequestDTO;
  13. use Harmonizely\Model\DomainAwareInterface;
  14. use Harmonizely\Model\Organization;
  15. use Harmonizely\Model\User;
  16. use Harmonizely\Model\UserInterface;
  17. use Harmonizely\Repository\Contract\IUserRepository;
  18. use Doctrine\ORM\NonUniqueResultException;
  19. class UserRepository extends EntityRepository implements IUserRepositoryDomainAwareEntityRepositoryInterface
  20. {
  21.     /**
  22.      * UserRepository constructor.
  23.      *
  24.      * @param ManagerRegistry $registry
  25.      */
  26.     public function __construct(ManagerRegistry $registry)
  27.     {
  28.         parent::__construct($registryUser::class);
  29.     }
  30.     public function findOneEnabledBySlug(string $slug): ?UserInterface
  31.     {
  32.         return $this->createQueryBuilder('u')
  33.             ->where('u.slug = :slug')
  34.             ->andWhere('u.enabled = 1')
  35.             ->setParameter('slug'$slug)
  36.             ->getQuery()
  37.             ->getOneOrNullResult();
  38.     }
  39.     /**
  40.      * @param string $slug
  41.      * @return UserInterface|null
  42.      * @throws NonUniqueResultException
  43.      */
  44.     public function findOneBySlug(string $slug): ?UserInterface
  45.     {
  46.         return $this->createQueryBuilder('u')
  47.             ->where('u.slug = :slug')
  48.             ->setParameter('slug'$slug)
  49.             ->getQuery()
  50.             ->getOneOrNullResult();
  51.     }
  52.     /**
  53.      * @param int $id
  54.      * @return UserInterface|null
  55.      * @throws NonUniqueResultException
  56.      */
  57.     public function findOneById(int $id): ?UserInterface
  58.     {
  59.         return $this->createQueryBuilder('u')
  60.             ->where('u.id = :id')
  61.             ->setParameter('id'$id)
  62.             ->getQuery()
  63.             ->getOneOrNullResult();
  64.     }
  65.     /**
  66.      * @param string $verificationCode
  67.      * @param string $confirmationToken
  68.      * @return UserInterface|null
  69.      * @throws NonUniqueResultException
  70.      */
  71.     public function findOneByVerificationCodeAndConfirmationToken(string $verificationCodestring $confirmationToken): ?UserInterface
  72.     {
  73.         return $this->createQueryBuilder('u')
  74.             ->where('u.verificationCode = :verificationCode')
  75.             ->andWhere('u.confirmationToken = :confirmationToken')
  76.             ->setParameters([
  77.                 'verificationCode' => $verificationCode,
  78.                 'confirmationToken' => $confirmationToken
  79.             ])
  80.             ->getQuery()
  81.             ->getOneOrNullResult();
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     public function countUsersBySlug(string $slug): int
  87.     {
  88.         return (int)$this->createQueryBuilder('u')
  89.             ->select('count(u)')
  90.             ->where('u.slug = :slug')
  91.             ->setParameter('slug'$slug)
  92.             ->getQuery()
  93.             ->getSingleScalarResult();
  94.     }
  95.     public function findUsersWithExpiredSubscriptions(): array
  96.     {
  97.         $now = new DateTime('now', new DateTimeZone('UTC'));
  98.         return $this->createQueryBuilder('u')
  99.             ->leftJoin('u.subscription''s')
  100.             ->where('s.expireAt <= :now')
  101.             ->setParameter('now'$now)
  102.             ->getQuery()
  103.             ->getResult();
  104.     }
  105.     public function findOneByDomain(string $domain): ?DomainAwareInterface
  106.     {
  107.         /** @var DomainAwareInterface $object */
  108.         $object $this->findOneBy(['domain' => $domain]);
  109.         return $object;
  110.     }
  111.     /**
  112.      * @param UserInterface $user
  113.      * @return UserInterface
  114.      * @throws ORMException
  115.      * @throws OptimisticLockException
  116.      */
  117.     public function save(UserInterface $user): UserInterface
  118.     {
  119.         $this->_em->persist($user);
  120.         $this->_em->flush();
  121.         return $user;
  122.     }
  123.     /**
  124.      * @param Organization $organization
  125.      * @return array|UserInterface[]
  126.      */
  127.     public function getList(Organization $organization): array
  128.     {
  129.         return $this->createQueryBuilder('u')
  130.             ->innerJoin('u.members''om')
  131.             ->andWhere('om.organization = :organization')
  132.             ->andWhere('u.enabled = 1')
  133.             ->setParameter('organization'$organization)
  134.             ->getQuery()
  135.             ->getResult();
  136.     }
  137.     /**
  138.      * @param string $email
  139.      * @return UserInterface|null
  140.      */
  141.     public function findByEmail(string $email): ?UserInterface
  142.     {
  143.         return $this->findOneBy(['email' => $email]);
  144.     }
  145.     /**
  146.      * @param string $token
  147.      * @return UserInterface|null
  148.      */
  149.     public function findByConfirmationToken(string $token): ?UserInterface
  150.     {
  151.         return $this->findOneBy(['confirmationToken' => $token]);
  152.     }
  153.     /**
  154.      * @param DateTime $createdAt
  155.      * @param string|null $template
  156.      * @return array|UserInterface[]
  157.      */
  158.     public function getListByCreatedAtForEmail(DateTime $createdAt, ?string $template): array
  159.     {
  160.         $query $this->createQueryBuilder('u')
  161.             ->where('DATE(u.createdAt) = :createdAt')
  162.             ->setParameter('createdAt'$createdAt->format('Y-m-d'))
  163.             ->andWhere('u.enabled = 1')
  164.             ->leftJoin('u.userEmailUnsubscribe''ueu')
  165.             ->andWhere('ueu.id IS NULL');
  166.         if ($template) {
  167.             $query->leftJoin('u.userEmailLog''uel',Join::WITH'uel.template = :template')
  168.                 ->andWhere('uel.id IS NULL')
  169.                 ->setParameter('template'$template);
  170.         }
  171.         return $query->getQuery()
  172.             ->getResult();
  173.     }
  174.     /**
  175.      * @param GetCustomerListRequestDTO $request
  176.      * @return IPaginator
  177.      */
  178.     public function getCustomerList(GetCustomerListRequestDTO $request): IPaginator
  179.     {
  180.         $qb $this->createQueryBuilder('u')
  181.             ->leftJoin('u.members''m')
  182.             ->leftJoin('m.organization''o');
  183.         switch ($request->getOrderField()) {
  184.             case 'email':
  185.             case 'fullName':
  186.             case 'slug':
  187.             case 'domain':
  188.             case 'createdAt':
  189.             case 'lastLogin':
  190.                 $qb->orderBy('u.' $request->getOrderField(), $request->getOrderDirection());
  191.                 break;
  192.             case 'phone':
  193.                 $qb->orderBy('u.phoneNumber'$request->getOrderDirection());
  194.                 break;
  195.             case 'isEnabled':
  196.                 $qb->orderBy('u.enabled'$request->getOrderDirection());
  197.                 break;
  198.             case 'organizationSlug':
  199.                 $qb->orderBy('o.slug'$request->getOrderDirection());
  200.                 break;
  201.             default:
  202.                 $qb->orderBy('u.createdAt'$request->getOrderDirection());
  203.                 break;
  204.         }
  205.         if ($request->getFilter()) {
  206.             if ($request->getFilter()->getSearch()) {
  207.                 $qb->andWhere('u.username LIKE :search OR u.email LIKE :search OR u.fullName LIKE :search OR u.slug LIKE :search')
  208.                     ->setParameter('search''%' $request->getFilter()->getSearch() . '%');
  209.             }
  210.             if ($request->getFilter()->getId()) {
  211.                 $qb->andWhere('u.id = :id')
  212.                     ->setParameter('id'$request->getFilter()->getId());
  213.             }
  214.             if ($request->getFilter()->getCreatedAtFrom()) {
  215.                 $qb->andWhere('DATE(u.createdAt) >= :createdAtFrom')
  216.                     ->setParameter('createdAtFrom'$request->getFilter()->getCreatedAtFrom());
  217.             }
  218.             if ($request->getFilter()->getCreatedAtTo()) {
  219.                 $qb->andWhere('DATE(u.createdAt) <= :createdAtTo')
  220.                     ->setParameter('createdAtTo'$request->getFilter()->getCreatedAtTo());
  221.             }
  222.             if ($request->getFilter()->getOrganizationId()) {
  223.                 $qb->andWhere('o.id = :organizationId')
  224.                     ->setParameter('organizationId'$request->getFilter()->getOrganizationId());
  225.             }
  226.         }
  227.         if (!$request->isSkipPagination()) {
  228.             $qb->setMaxResults($request->getOnPage())
  229.                 ->setFirstResult($request->getOffset());
  230.         }
  231.         return new Paginator($qb->getQuery());
  232.     }
  233.     /**
  234.      * @return array|UserInterface[]
  235.      */
  236.     public function getListWithDeviceToken(): array
  237.     {
  238.         return $this->createQueryBuilder('u')
  239.             ->innerJoin('u.userDeviceToken''udt')
  240.             ->getQuery()
  241.             ->getResult();
  242.     }
  243. }