chill-bundles/src/Bundle/ChillThirdPartyBundle/Repository/ThirdPartyACLAwareRepository.php

80 lines
2.3 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\ThirdPartyBundle\Repository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Security;
final class ThirdPartyACLAwareRepository implements ThirdPartyACLAwareRepositoryInterface
{
private AuthorizationHelper $authorizationHelper;
private Security $security;
private ThirdPartyRepository $thirdPartyRepository;
public function __construct(Security $security, AuthorizationHelper $authorizationHelper, ThirdPartyRepository $thirdPartyRepository)
{
$this->security = $security;
$this->authorizationHelper = $authorizationHelper;
$this->thirdPartyRepository = $thirdPartyRepository;
}
public function buildQuery(?string $filterString = null): QueryBuilder
{
$qb = $this->thirdPartyRepository->createQueryBuilder('tp');
$qb->leftJoin('tp.parent', 'parent')
->andWhere($qb->expr()->andX(
'tp.active = \'TRUE\'',
$qb->expr()->orX($qb->expr()->isNull('parent'), 'parent.active = \'TRUE\'')
));
if (null !== $filterString) {
$qb->andWhere($qb->expr()->like('tp.canonicalized', 'LOWER(UNACCENT(:filterString))'))
->setParameter('filterString', '%' . $filterString . '%');
}
return $qb;
}
public function countThirdParties(
string $role,
?string $filterString
): int {
$qb = $this->buildQuery($filterString);
$qb->select('count(tp)');
return $qb->getQuery()->getSingleScalarResult();
}
public function listThirdParties(
string $role,
?string $filterString,
?array $orderBy = [],
?int $limit = null,
?int $offset = null
): array {
$qb = $this->buildQuery($filterString);
foreach ($orderBy as $sort => $direction) {
$qb->addOrderBy('tp.' . $sort, $direction);
}
$qb->setFirstResult($offset)
->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
}