src/Repository/OrganizationRepository.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Repository;
  4. use Doctrine\Common\Persistence\ManagerRegistry;
  5. use Doctrine\DBAL\LockMode;
  6. use Doctrine\ORM\OptimisticLockException;
  7. use Doctrine\ORM\ORMException;
  8. use Harmonizely\Core\ORM\Tools\Contract\IPaginator;
  9. use Harmonizely\Core\ORM\Tools\Paginator;
  10. use Harmonizely\DTO\SuperAdmin\Organization\Request\GetOrganizationListRequestDTO;
  11. use Harmonizely\Model\DomainAwareInterface;
  12. use Harmonizely\Model\Organization;
  13. use Harmonizely\Repository\Contract\IOrganizationRepository;
  14. class OrganizationRepository extends EntityRepository implements DomainAwareEntityRepositoryInterfaceIOrganizationRepository
  15. {
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryOrganization::class);
  19.     }
  20.     /**
  21.      * @param Organization $organization
  22.      * @return Organization
  23.      * @throws ORMException
  24.      * @throws OptimisticLockException
  25.      */
  26.     public function save(Organization $organization): Organization
  27.     {
  28.         $this->_em->persist($organization);
  29.         $this->_em->flush();
  30.         return $organization;
  31.     }
  32.     public function findOneByDomain(string $domain): ?DomainAwareInterface
  33.     {
  34.         /** @var DomainAwareInterface $object */
  35.         $object $this->findOneBy(['domain' => $domain]);
  36.         return $object;
  37.     }
  38.     /**
  39.      * @param Organization $organization
  40.      * @return void
  41.      * @throws ORMException
  42.      * @throws OptimisticLockException
  43.      */
  44.     public function delete(Organization $organization): void
  45.     {
  46.         $this->_em->remove($organization);
  47.         $this->_em->flush();
  48.     }
  49.     /**
  50.      * @param Organization $organization
  51.      * @return Organization
  52.      */
  53.     public function findWithBlockTransaction(Organization $organization): Organization
  54.     {
  55.         return $this->find($organization->getId(), LockMode::PESSIMISTIC_WRITE);
  56.     }
  57.     /**
  58.      * @param GetOrganizationListRequestDTO $request
  59.      * @return IPaginator
  60.      */
  61.     public function getOrganizationList(GetOrganizationListRequestDTO $request): IPaginator
  62.     {
  63.         $qb $this->createQueryBuilder('o');
  64.         switch ($request->getOrderField()) {
  65.             case 'id':
  66.             case 'slug':
  67.             case 'name':
  68.             case 'createdAt':
  69.             case 'domain':
  70.                 $qb->orderBy('o.' $request->getOrderField(), $request->getOrderDirection());
  71.                 break;
  72.             default:
  73.                 $qb->orderBy('o.createdAt'$request->getOrderDirection());
  74.                 break;
  75.         }
  76.         if ($request->getFilter()) {
  77.             if ($request->getFilter()->getSearch()) {
  78.                 $qb->andWhere('o.name LIKE :search OR o.slug LIKE :search')
  79.                     ->setParameter('search''%' $request->getFilter()->getSearch() . '%');
  80.             }
  81.             if ($request->getFilter()->getId()) {
  82.                 $qb->andWhere('o.id = :id')
  83.                     ->setParameter('id'$request->getFilter()->getId());
  84.             }
  85.             if ($request->getFilter()->getCreatedAtFrom()) {
  86.                 $qb
  87.                     ->andWhere('DATE(o.createdAt) >= :createdAtFrom')
  88.                     ->setParameter('createdAtFrom'$request->getFilter()->getCreatedAtFrom());
  89.             }
  90.             if ($request->getFilter()->getCreatedAtTo()) {
  91.                 $qb
  92.                     ->andWhere('DATE(o.createdAt) <= :createdAtTo')
  93.                     ->setParameter('createdAtTo'$request->getFilter()->getCreatedAtTo());
  94.             }
  95.         }
  96.         if (!$request->isSkipPagination()) {
  97.             $qb->setMaxResults($request->getOnPage())
  98.                 ->setFirstResult($request->getOffset());
  99.         }
  100.         return new Paginator($qb->getQuery());
  101.     }
  102. }