src/FMT/Data/Repository/UserSchoolRepository.php line 27

Open in your IDE?
  1. <?php
  2. namespace FMT\Data\Repository;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\NonUniqueResultException;
  6. use Doctrine\ORM\Query;
  7. use Doctrine\ORM\QueryBuilder;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use FMT\Data\Entity\User;
  10. use FMT\Data\Entity\UserSchool;
  11. use FMT\Domain\Repository\UserSchoolRepositoryInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. /**
  14.  * @method UserSchool|null find($id, $lockMode = null, $lockVersion = null)
  15.  * @method UserSchool|null findOneBy(array $criteria, array $orderBy = null)
  16.  * @method UserSchool[]    findAll()
  17.  * @method UserSchool[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  18.  */
  19. class UserSchoolRepository extends DoctrineRepository implements UserSchoolRepositoryInterface
  20. {
  21.     private EntityManagerInterface $entityManager;
  22.     
  23.     
  24.     public function __construct(ManagerRegistry $registry,EntityManagerInterface $entityManager)
  25.     {
  26.         parent::__construct($registryUserSchool::class);
  27.         $this->entityManager $entityManager;
  28.     }
  29.     public function findWithMajors(int $schoolId): ?UserSchool
  30.     {
  31.         return $this->createQueryBuilder('us')
  32.             ->leftJoin('us.userMajors''um')
  33.             ->addSelect('um')
  34.             ->where('us.id = :id')
  35.             ->setParameter('id'$schoolId)
  36.             ->getQuery()
  37.             ->getOneOrNullResult();
  38.     }
  39.     /**
  40.      * @param array $criteria
  41.      * @return array|UserSchool[]
  42.      * @deprecated
  43.      */
  44.     public function findAllBy(array $criteria)
  45.     {
  46.         return $this->findBy($criteria);
  47.     }
  48.     /**
  49.      * @param bool $activeOnly
  50.      * @return ArrayCollection
  51.      */
  52.     public function getSchoolsCollection(?UserInterface $user$activeOnly false): ArrayCollection
  53.     {
  54.         $userSchool $user instanceof UserInterface $user->getProfile()->getSchool() : null;
  55.         $result $this->getSchoolsQuery($activeOnly)
  56.             ->orWhere('us.id = :userSchool')
  57.             ->setParameter('userSchool'$userSchool)
  58.             ->getQuery()
  59.             ->execute();
  60.         return is_array($result) ? new ArrayCollection($result) : $result;
  61.     }
  62.     /**
  63.      * @param bool $activeOnly
  64.      * @return QueryBuilder
  65.      */
  66.     private function getSchoolsQuery($activeOnly false)
  67.     {
  68.         $qb $this->createQueryBuilder("us");
  69.         if ($activeOnly) {
  70.             $qb->where("us.active = true");
  71.         }
  72.         return $qb;
  73.     }
  74.     /**
  75.      * @param bool $activeOnly
  76.      * @return array|ArrayCollection|int|string
  77.      */
  78.     public function getSchools($activeOnly false)
  79.     {
  80.         return $this->getSchoolsQuery($activeOnly)->getQuery()->getResult();
  81.     }
  82.     public function getSchoolForTransfer(): ?UserSchool
  83.     {
  84.         $query $this->createQueryBuilder('userSchool');
  85.         return $query->leftJoin('userSchool.userSchoolTransferAttempts''uu')
  86.             ->leftJoin('userSchool.userSchoolSettings''us')
  87.             ->orderBy('uu.updatedAt')
  88.             ->where('us.apiId IS NOT NULL')
  89.             ->andWhere('us.accessToken IS NOT NULL')
  90.             ->andWhere('us.stripeCustomerId IS NOT NULL')
  91.             ->andWhere('userSchool.active = true')
  92.             ->setMaxResults(1)
  93.             ->getQuery()
  94.             ->getOneOrNullResult();
  95.     }
  96.     /**
  97.      * @param int $bookstoreId
  98.      * @return UserSchool|null
  99.      * @throws NonUniqueResultException
  100.      */
  101.     public function getActiveSchoolByBookstore(int $bookstoreId): ?UserSchool
  102.     {
  103.         return $this->createQueryBuilder('userSchool')
  104.             ->where('userSchool.active = true')
  105.             ->andWhere('userSchool.bookstoreId = :bookstoreId')
  106.             ->setParameter('bookstoreId'$bookstoreId)
  107.             ->getQuery()
  108.             ->getOneOrNullResult();
  109.     }
  110. }