src/AdminBundle/Repository/UserRepository.php line 26

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Repository;
  3. use AdminBundle\Entity\User;
  4. use DateTime;
  5. class UserRepository extends BaseRepository
  6. {
  7. const BATCH_SIZE = 1000;
  8. public function findEnabledByRoleQb(string $role)
  9. {
  10. $qb = $this->createQueryBuilder('u');
  11. return $qb
  12. ->select('u.id, u.apiKey')
  13. ->where('u.enabled = :enabled')
  14. ->andWhere($qb->expr()->like('u.roles', ':role'))
  15. ->andWhere($qb->expr()->isNotNull('u.apiKey'))
  16. ->setParameter('enabled', true)
  17. ->setParameter('role', "%${role}%")
  18. ;
  19. }
  20. public function findEnabledByRole(string $role, int $firstResult = null, int $maxResults = null)
  21. {
  22. $qb = $this->findEnabledByRoleQb($role);
  23. if ($firstResult) {
  24. $qb->setFirstResult($firstResult);
  25. }
  26. if ($maxResults) {
  27. $qb->setMaxResults($maxResults);
  28. }
  29. return $qb->getQuery()->getArrayResult();
  30. }
  31. public function findActiveByDateAndRole(string $role, bool $inactive = null)
  32. {
  33. $qb = $this->findEnabledByRoleQb($role);
  34. if ($role === User::ROLE_CLIENT) {
  35. return $qb
  36. ->andWhere($qb->expr()->isNotNull('u.lastLogin'))
  37. ->andWhere($qb->expr()->between('u.lastLogin', ':startDate', ':endDate'))
  38. ->setParameter('startDate', new DateTime('-30 days'))
  39. ->setParameter('endDate', new DateTime())
  40. ->getQuery()
  41. ->getArrayResult();
  42. }
  43. $qb->innerJoin('u.driver', 'd');
  44. if ($inactive) {
  45. return $qb
  46. ->andWhere($qb->expr()->lt('d.lastUpdatePozition', ':date'))
  47. ->setParameter('date', new DateTime('-7 days'))
  48. ->getQuery()
  49. ->getArrayResult()
  50. ;
  51. }
  52. return $qb
  53. ->andWhere($qb->expr()->between('d.lastUpdatePozition', ':startDate', ':endDate'))
  54. ->setParameter('startDate', new DateTime('-7 days'))
  55. ->setParameter('endDate', new DateTime())
  56. ->getQuery()
  57. ->getArrayResult()
  58. ;
  59. }
  60. public function findClientsHavingBookings(int $bookingsCount = 1)
  61. {
  62. $qb = $this->findEnabledByRoleQb(User::ROLE_CLIENT);
  63. return $qb
  64. ->addSelect('COUNT(b.id) as bookingsCount')
  65. ->innerJoin('u.clientBookings', 'b')
  66. ->groupBy('u.id')
  67. ->having($qb->expr()->eq('bookingsCount', ':bookingsCount'))
  68. ->setParameter('bookingsCount', $bookingsCount)
  69. ->getQuery()
  70. ->getArrayResult()
  71. ;
  72. }
  73. /**
  74. * Get the users without an apiKey QueryBuilder.
  75. *
  76. * @return QueryBuilder
  77. */
  78. protected function usersNoApiKey() {
  79. return $this->createQueryBuilder('u')->where("u.apiKey is NULL or u.apiKey = ''");
  80. }
  81. /**
  82. * Get the total number of users without an apiKey.
  83. *
  84. * @return integer
  85. */
  86. public function usersNoApiKeyCount() {
  87. return $this->usersNoApiKey()->select('count(u.id)')->getQuery()->getSingleScalarResult();
  88. }
  89. /**
  90. * Get the a batch of users without an apiKey.
  91. *
  92. * @return array
  93. */
  94. public function usersNoApiKeyData() {
  95. $qb = $this->usersNoApiKey()->select('u')->setMaxResults(self::BATCH_SIZE);
  96. return $qb->getQuery()->getResult();
  97. }
  98. }