<?php
declare(strict_types=1);
namespace Harmonizely\Repository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Harmonizely\Core\ORM\Tools\Contract\IPaginator;
use Harmonizely\Core\ORM\Tools\Paginator;
use Harmonizely\DTO\SuperAdmin\Organization\Request\GetOrganizationListRequestDTO;
use Harmonizely\Model\DomainAwareInterface;
use Harmonizely\Model\Organization;
use Harmonizely\Repository\Contract\IOrganizationRepository;
class OrganizationRepository extends EntityRepository implements DomainAwareEntityRepositoryInterface, IOrganizationRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Organization::class);
}
/**
* @param Organization $organization
* @return Organization
* @throws ORMException
* @throws OptimisticLockException
*/
public function save(Organization $organization): Organization
{
$this->_em->persist($organization);
$this->_em->flush();
return $organization;
}
public function findOneByDomain(string $domain): ?DomainAwareInterface
{
/** @var DomainAwareInterface $object */
$object = $this->findOneBy(['domain' => $domain]);
return $object;
}
/**
* @param Organization $organization
* @return void
* @throws ORMException
* @throws OptimisticLockException
*/
public function delete(Organization $organization): void
{
$this->_em->remove($organization);
$this->_em->flush();
}
/**
* @param Organization $organization
* @return Organization
*/
public function findWithBlockTransaction(Organization $organization): Organization
{
return $this->find($organization->getId(), LockMode::PESSIMISTIC_WRITE);
}
/**
* @param GetOrganizationListRequestDTO $request
* @return IPaginator
*/
public function getOrganizationList(GetOrganizationListRequestDTO $request): IPaginator
{
$qb = $this->createQueryBuilder('o');
switch ($request->getOrderField()) {
case 'id':
case 'slug':
case 'name':
case 'createdAt':
case 'domain':
$qb->orderBy('o.' . $request->getOrderField(), $request->getOrderDirection());
break;
default:
$qb->orderBy('o.createdAt', $request->getOrderDirection());
break;
}
if ($request->getFilter()) {
if ($request->getFilter()->getSearch()) {
$qb->andWhere('o.name LIKE :search OR o.slug LIKE :search')
->setParameter('search', '%' . $request->getFilter()->getSearch() . '%');
}
if ($request->getFilter()->getId()) {
$qb->andWhere('o.id = :id')
->setParameter('id', $request->getFilter()->getId());
}
if ($request->getFilter()->getCreatedAtFrom()) {
$qb
->andWhere('DATE(o.createdAt) >= :createdAtFrom')
->setParameter('createdAtFrom', $request->getFilter()->getCreatedAtFrom());
}
if ($request->getFilter()->getCreatedAtTo()) {
$qb
->andWhere('DATE(o.createdAt) <= :createdAtTo')
->setParameter('createdAtTo', $request->getFilter()->getCreatedAtTo());
}
}
if (!$request->isSkipPagination()) {
$qb->setMaxResults($request->getOnPage())
->setFirstResult($request->getOffset());
}
return new Paginator($qb->getQuery());
}
}