crudification + corrections on thirdparty

This commit is contained in:
2021-10-04 18:25:49 +02:00
parent cc7e38194f
commit 05b9476a71
16 changed files with 313 additions and 212 deletions

View File

@@ -2,22 +2,52 @@
namespace Chill\ThirdPartyBundle\Repository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Security;
/**
* @Author Mathieu Jaumotte mathieu.jaumotte@champs-libres.coop
*/
class ThirdPartyACLAwareRepository implements ThirdPartyACLAwareRepositoryInterface
final class ThirdPartyACLAwareRepository implements ThirdPartyACLAwareRepositoryInterface
{
private Security $security;
private AuthorizationHelper $authorizationHelper;
private ThirdPartyRepository $thirdPartyRepository;
public function findByThirdparty(
ThirdParty $thirdparty,
string $role,
?array $orderBy = [],
int $limit = null,
int $offset = null
public function __construct(Security $security, AuthorizationHelper $authorizationHelper, ThirdPartyRepository $thirdPartyRepository)
{
$this->security = $security;
$this->authorizationHelper = $authorizationHelper;
$this->thirdPartyRepository = $thirdPartyRepository;
}
public function listThirdParties(
string $role,
?array $orderBy = [],
?int $limit = null,
?int $offset = null
): array {
$qb = $this->buildQuery($role);
// TODO: Implement findByThirdparty() method.
foreach ($orderBy as $sort => $direction) {
$qb->addOrderBy('tp.'.$sort, $direction);
}
$qb->setFirstResult($offset)
->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
public function countThirdParties(
string $role
): int {
$qb = $this->buildQuery($role);
$qb->select('count(tp)');
return $qb->getQuery()->getSingleScalarResult();
}
public function buildQuery(): QueryBuilder {
return $this->thirdPartyRepository->createQueryBuilder('tp');
}
}

View File

@@ -6,8 +6,16 @@ use Chill\ThirdPartyBundle\Entity\ThirdParty;
interface ThirdPartyACLAwareRepositoryInterface
{
public function findByThirdparty(
ThirdParty $thirdparty,
public function countThirdParties(string $role): int;
/**
* @param string $role
* @param array|null $orderBy
* @param int|null $limit
* @param int|null $offset
* @return array|ThirdParty[]
*/
public function listThirdParties(
string $role,
?array $orderBy = [],
int $limit = null,

View File

@@ -2,23 +2,26 @@
namespace Chill\ThirdPartyBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\Persistence\ObjectRepository;
class ThirdPartyRepository extends ServiceEntityRepository
final class ThirdPartyRepository implements ObjectRepository
{
public function __construct(ManagerRegistry $registry)
private EntityRepository $repository;
public function __construct(EntityManagerInterface $em)
{
parent::__construct($registry, ThirdParty::class);
$this->repository = $em->getRepository(ThirdParty::class);
}
/**
* count amongst parties associated to $centers, with $terms parameters
*
*
* @param array $centers
* @param type $terms
* @return int
@@ -27,24 +30,24 @@ class ThirdPartyRepository extends ServiceEntityRepository
{
$qb = $this->buildQuery($centers, $terms);
$qb->select('COUNT(tp)');
return $qb->getQuery()->getSingleScalarResult();
}
/**
* Search amongst parties associated to $centers, with $terms parameters
*
*
* Different format for return:
* - ['entity']: return the entity hydrated as objects
* - ['array', [ DQL ]: return objects hydrated as entity, with
* - ['array', [ DQL ]: return objects hydrated as entity, with
* an array describing the fields as DQL.
*
*
* supported terms:
*
*
* - name or _default: containing the name (name LIKE %string%)
* - is_active: is active = true / false
* - types: an array of types
*
*
* @param array $centers
* @param int $firstResult
* @param int $maxResults
@@ -74,43 +77,43 @@ class ThirdPartyRepository extends ServiceEntityRepository
break;
default:
throw new \DomainException("This return format is invalid");
}
}
$qb->setFirstResult($firstResult)
->setMaxResults($maxResults);
return $qb->getQuery()->getResult();
}
protected function createMemberOfCentersQuery($centers): QueryBuilder
{
$qb = $this->createQueryBuilder('tp');
$or = $qb->expr()->orX();
foreach ($centers as $center) {
$or->add($qb->expr()->isMemberOf(':center_'.$center->getId(), 'tp.centers'));
$qb->setParameter('center_'.$center->getId(), $center);
}
$qb->where($or);
return $qb;
}
protected function buildQuery($centers, $terms): QueryBuilder
{
$qb = $this->createMemberOfCentersQuery($centers);
$this->setNameCondition($qb, $terms);
$this->setTypesCondition($qb, $terms);
$this->setIsActiveCondition($qb, $terms);
return $qb;
}
/**
* Add parameters to filter by containing $terms["name"] or
* Add parameters to filter by containing $terms["name"] or
* $terms["_default"]
*
*
* @param QueryBuilder $qb
* @param array $terms
*/
@@ -125,7 +128,7 @@ class ThirdPartyRepository extends ServiceEntityRepository
$qb->setParameter('name', '%'.$term.'%');
}
}
protected function setTypesCondition(QueryBuilder $qb, array $terms)
{
if (\array_key_exists('types', $terms)) {
@@ -137,14 +140,55 @@ class ThirdPartyRepository extends ServiceEntityRepository
$qb->andWhere($orx);
}
}
protected function setIsActiveCondition(QueryBuilder $qb, array $terms)
{
if (\array_key_exists('is_active', $terms)) {
$qb->andWhere(
$terms['is_active'] ? $qb->expr()->eq('tp.active', "'TRUE'") :
$terms['is_active'] ? $qb->expr()->eq('tp.active', "'TRUE'") :
$qb->expr()->eq('tp.active', "'FALSE'")
);
}
}
public function find($id): ?ThirdParty
{
return $this->repository->find($id);
}
/**
* @return array|ThirdParty[]
*/
public function findAll(): array
{
return $this->repository->findAll();
}
/**
* @param array $criteria
* @param array|null $orderBy
* @param null $limit
* @param null $offset
* @return array|ThirdParty[]
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?ThirdParty
{
return $this->repository->findOneBy($criteria);
}
public function getClassName(): string
{
return ThirdParty::class;
}
public function createQueryBuilder(string $alias, ?string $indexBy = null): QueryBuilder
{
return $this->repository->createQueryBuilder($alias, $indexBy);
}
}