<?php
namespace FMT\Data\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use FMT\Data\Entity\User;
use FMT\Data\Entity\UserSchool;
use FMT\Domain\Repository\UserSchoolRepositoryInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @method UserSchool|null find($id, $lockMode = null, $lockVersion = null)
* @method UserSchool|null findOneBy(array $criteria, array $orderBy = null)
* @method UserSchool[] findAll()
* @method UserSchool[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserSchoolRepository extends DoctrineRepository implements UserSchoolRepositoryInterface
{
private EntityManagerInterface $entityManager;
public function __construct(ManagerRegistry $registry,EntityManagerInterface $entityManager)
{
parent::__construct($registry, UserSchool::class);
$this->entityManager = $entityManager;
}
public function findWithMajors(int $schoolId): ?UserSchool
{
return $this->createQueryBuilder('us')
->leftJoin('us.userMajors', 'um')
->addSelect('um')
->where('us.id = :id')
->setParameter('id', $schoolId)
->getQuery()
->getOneOrNullResult();
}
/**
* @param array $criteria
* @return array|UserSchool[]
* @deprecated
*/
public function findAllBy(array $criteria)
{
return $this->findBy($criteria);
}
/**
* @param bool $activeOnly
* @return ArrayCollection
*/
public function getSchoolsCollection(?UserInterface $user, $activeOnly = false): ArrayCollection
{
$userSchool = $user instanceof UserInterface ? $user->getProfile()->getSchool() : null;
$result = $this->getSchoolsQuery($activeOnly)
->orWhere('us.id = :userSchool')
->setParameter('userSchool', $userSchool)
->getQuery()
->execute();
return is_array($result) ? new ArrayCollection($result) : $result;
}
/**
* @param bool $activeOnly
* @return QueryBuilder
*/
private function getSchoolsQuery($activeOnly = false)
{
$qb = $this->createQueryBuilder("us");
if ($activeOnly) {
$qb->where("us.active = true");
}
return $qb;
}
/**
* @param bool $activeOnly
* @return array|ArrayCollection|int|string
*/
public function getSchools($activeOnly = false)
{
return $this->getSchoolsQuery($activeOnly)->getQuery()->getResult();
}
public function getSchoolForTransfer(): ?UserSchool
{
$query = $this->createQueryBuilder('userSchool');
return $query->leftJoin('userSchool.userSchoolTransferAttempts', 'uu')
->leftJoin('userSchool.userSchoolSettings', 'us')
->orderBy('uu.updatedAt')
->where('us.apiId IS NOT NULL')
->andWhere('us.accessToken IS NOT NULL')
->andWhere('us.stripeCustomerId IS NOT NULL')
->andWhere('userSchool.active = true')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* @param int $bookstoreId
* @return UserSchool|null
* @throws NonUniqueResultException
*/
public function getActiveSchoolByBookstore(int $bookstoreId): ?UserSchool
{
return $this->createQueryBuilder('userSchool')
->where('userSchool.active = true')
->andWhere('userSchool.bookstoreId = :bookstoreId')
->setParameter('bookstoreId', $bookstoreId)
->getQuery()
->getOneOrNullResult();
}
}