cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,58 +1,65 @@
<?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.
*/
namespace Chill\TaskBundle\Repository;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use LogicException;
use Symfony\Component\Security\Core\Role\Role;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Doctrine\DBAL\Types\Types;
use Chill\MainBundle\Entity\Center;
use UnexpectedValueException;
use function array_key_exists;
/**
* Class SingleTaskRepository
*
* @package Chill\TaskBundle\Repository
* Class SingleTaskRepository.
*/
class SingleTaskRepository extends EntityRepository
{
public const DATE_STATUS_CURRENT = 'current';
const DATE_STATUS_ENDED = 'ended';
const DATE_STATUS_WARNING = 'warning';
const DATE_STATUS_CURRENT = 'current';
const DATE_STATUS_NOT_STARTED = 'not_started';
public const DATE_STATUS_ENDED = 'ended';
const DATE_STATUSES = [
public const DATE_STATUS_NOT_STARTED = 'not_started';
public const DATE_STATUS_WARNING = 'warning';
public const DATE_STATUSES = [
self::DATE_STATUS_ENDED,
self::DATE_STATUS_WARNING,
self::DATE_STATUS_CURRENT,
self::DATE_STATUS_NOT_STARTED
self::DATE_STATUS_NOT_STARTED,
];
/**
*
* @var AuthorizationHelper
*/
protected $authorizationHelper;
public function setAuthorizationHelper(AuthorizationHelper $authorizationHelper)
{
$this->authorizationHelper = $authorizationHelper;
}
/**
* Count the tasks for given parameters.
*
* The parameters are describe in @see SingleTaskRepository::filterByParameters.
*
* @see SingleTaskRepository::filterByParameters
*
* @param array $params
* @param User $currentUser
*
* @return int
*/
public function countByParameters($params, User $currentUser = null)
public function countByParameters($params, ?User $currentUser = null)
{
$qb = $this->createQueryBuilder('st')
->select('COUNT(st)');
@@ -61,8 +68,7 @@ class SingleTaskRepository extends EntityRepository
return (int) $qb
->getQuery()
->getSingleScalarResult()
;
->getSingleScalarResult();
}
/**
@@ -80,9 +86,7 @@ class SingleTaskRepository extends EntityRepository
* - `types`: string[] an array of task types
*
* @param type $params
* @param User $currentUser
* @param int $firstResult
* @param int $maxResults
*
* @return type
*/
public function findByParameters($params, User $currentUser, int $firstResult = 0, int $maxResults = 50)
@@ -93,68 +97,16 @@ class SingleTaskRepository extends EntityRepository
$qb
->setMaxResults($maxResults)
->setFirstResult($firstResult)
;
->setFirstResult($firstResult);
return $qb
->getQuery()
->getResult()
;
->getResult();
}
protected function buildQuery(QueryBuilder $qb, $params, User $currentUser = null)
public function setAuthorizationHelper(AuthorizationHelper $authorizationHelper)
{
if (NULL !== $currentUser) {
$this->buildACLQuery($qb, $currentUser);
}
if (\array_key_exists('person', $params) and !empty($params['person'])) {
$qb->andWhere($qb->expr()->eq('st.person', ':person'));
$qb->setParameter('person', $params['person']);
} elseif (\array_key_exists('center', $params)) {
if ($params['center'] instanceof Center) {
$qb->join('st.person', 'person');
$qb->andWhere($qb->expr()->eq('person.center', ':center'));
$qb->setParameter('center', $params['center']);
} else {
throw new \UnexpectedValueException("params 'center' should be an instance of ".Center::class);
}
}
if (\array_key_exists('unassigned', $params) and $params['unassigned'] === true) {
if (\array_key_exists('user', $params) and !empty($params['user'])) {
throw new \UnexpectedValueException("You should not require for "
. "unassigned tasks and tasks assigned to some user.");
}
$qb->andWhere($qb->expr()->isNull('st.assignee'));
}
if (\array_key_exists('user', $params) and !empty($params['user'])) {
$qb->andWhere($qb->expr()->eq('st.assignee', ':user'));
$qb->setParameter('user', $params['user']);
}
if (\array_key_exists('scope', $params) and !empty($params['scope'])) {
$qb->andWhere($qb->expr()->eq('st.circle', ':scope'));
$qb->setParameter('scope', $params['scope']);
}
if (\array_key_exists('types', $params) && $params['types'] !== NULL) {
if (count($params['types']) > 0) {
$qb->andWhere($qb->expr()->in('st.type', ':types'));
$qb->setParameter('types', $params['types']);
}
}
if (\array_key_exists('date_status', $params) and !empty($params['date_status'])) {
$this->addTypeFilter($qb, $params);
}
if (\array_key_exists('is_closed', $params)) {
$qb->andWhere($this->buildIsClosed($qb, !$params['is_closed']));
}
$this->authorizationHelper = $authorizationHelper;
}
protected function addTypeFilter(QueryBuilder $qb, $params)
@@ -164,15 +116,15 @@ class SingleTaskRepository extends EntityRepository
switch ($params['date_status']) {
case self::DATE_STATUS_ENDED:
$andWhere
->add($this->buildNowIsAfterEndDate($qb))
;
->add($this->buildNowIsAfterEndDate($qb));
break;
case self::DATE_STATUS_WARNING:
$andWhere
->add($this->buildNowIsAfterEndDate($qb, true))
->add($this->buildNowIsAfterWarningDate($qb))
;
->add($this->buildNowIsAfterWarningDate($qb));
break;
case self::DATE_STATUS_CURRENT:
@@ -180,125 +132,174 @@ class SingleTaskRepository extends EntityRepository
$andWhere
->add($this->buildNowIsAfterEndDate($qb, true))
->add($this->buildNowIsAfterWarningDate($qb, true))
->add($this->buildNowIsAfterStartDate($qb, false))
;
->add($this->buildNowIsAfterStartDate($qb, false));
break;
case self::DATE_STATUS_NOT_STARTED:
$andWhere
->add($this->buildNowIsAfterEndDate($qb, true))
->add($this->buildNowIsAfterWarningDate($qb, true))
->add($this->buildNowIsAfterStartDate($qb, true))
;
->add($this->buildNowIsAfterStartDate($qb, true));
}
$qb->setParameter('now', new \DateTime('today'), Types::DATE_MUTABLE);
$qb->setParameter('now', new DateTime('today'), Types::DATE_MUTABLE);
$qb->andWhere($andWhere);
}
private function buildNowIsAfterEndDate(QueryBuilder $qb, $negative = false)
{
if ($negative === false) {
return $qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->lte('st.endDate', ':now'))
;
} else {
return $qb->expr()->orX()
->add(
$qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->gt('st.endDate', ':now'))
)
->add($qb->expr()->isNull('st.endDate'))
;
}
}
private function buildNowIsAfterWarningDate(QueryBuilder $qb, bool $negative = false)
{
if ($negative === false) {
return $qb->expr()->andX()
->add($qb->expr()->lte(
$qb->expr()->diff('st.endDate', 'st.warningInterval'), ':now'
)
);
} else {
return $qb->expr()->orX()
->add(
$qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->isNotNull('st.warningInterval'))
->add($qb->expr()->gt(
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
':now'
)
)
)
->add($qb->expr()->isNull('st.endDate'))
->add($qb->expr()->isNull('st.warningInterval'))
;
}
}
private function buildNowIsAfterStartDate(QueryBuilder $qb, bool $negative = false)
{
if ($negative === false) {
return $qb->expr()->orX()
->add($qb->expr()->lte('st.startDate', ':now'))
->add($qb->expr()->isNull('st.startDate'))
;
} else {
return
$qb->expr()->andX()
->add($qb->expr()->gt('st.startDate', ':now'))
->add($qb->expr()->isNotNull('st.startDate'))
;
}
}
private function buildIsClosed(QueryBuilder $qb, bool $negative = false)
{
if ($negative === false) {
return $qb->expr()->eq('st.closed', "'TRUE'");
} else {
return $qb->expr()->eq('st.closed', "'FALSE'");
}
}
protected function buildACLQuery(QueryBuilder $qb, User $currentUser)
{
if (NULL === $this->authorizationHelper) {
throw new \LogicException("Injecting the authorization helper is "
. "required to run this query. Please use dependency injection "
. "to initialize this repository or use the method "
. "`setAuthorizationHelper`");
if (null === $this->authorizationHelper) {
throw new LogicException('Injecting the authorization helper is '
. 'required to run this query. Please use dependency injection '
. 'to initialize this repository or use the method '
. '`setAuthorizationHelper`');
}
$role = new Role(TaskVoter::SHOW);
$qb->join('st.person', 'p');
$centers = $this->authorizationHelper
->getReachableCenters($currentUser, $role)
;
->getReachableCenters($currentUser, $role);
$i = 0;
$where = $qb->expr()->orX();
foreach($centers as $center) {
foreach ($centers as $center) {
$circles = $this->authorizationHelper
->getReachableCircles($currentUser, $role, $center);
$centerWhere = $qb->expr()->andX();
$centerWhere->add($qb->expr()->eq('p.center', ':center_'.$i));
$qb->setParameter('center_'.$i, $center);
$centerWhere->add($qb->expr()->in('st.circle', ':circles_'.$i));
$qb->setParameter('circles_'.$i, $circles);
$centerWhere->add($qb->expr()->eq('p.center', ':center_' . $i));
$qb->setParameter('center_' . $i, $center);
$centerWhere->add($qb->expr()->in('st.circle', ':circles_' . $i));
$qb->setParameter('circles_' . $i, $circles);
$where->add($centerWhere);
$i ++;
++$i;
}
$qb->where($where);
}
protected function buildQuery(QueryBuilder $qb, $params, ?User $currentUser = null)
{
if (null !== $currentUser) {
$this->buildACLQuery($qb, $currentUser);
}
if (array_key_exists('person', $params) and !empty($params['person'])) {
$qb->andWhere($qb->expr()->eq('st.person', ':person'));
$qb->setParameter('person', $params['person']);
} elseif (array_key_exists('center', $params)) {
if ($params['center'] instanceof Center) {
$qb->join('st.person', 'person');
$qb->andWhere($qb->expr()->eq('person.center', ':center'));
$qb->setParameter('center', $params['center']);
} else {
throw new UnexpectedValueException("params 'center' should be an instance of " . Center::class);
}
}
if (array_key_exists('unassigned', $params) and true === $params['unassigned']) {
if (array_key_exists('user', $params) and !empty($params['user'])) {
throw new UnexpectedValueException('You should not require for '
. 'unassigned tasks and tasks assigned to some user.');
}
$qb->andWhere($qb->expr()->isNull('st.assignee'));
}
if (array_key_exists('user', $params) and !empty($params['user'])) {
$qb->andWhere($qb->expr()->eq('st.assignee', ':user'));
$qb->setParameter('user', $params['user']);
}
if (array_key_exists('scope', $params) and !empty($params['scope'])) {
$qb->andWhere($qb->expr()->eq('st.circle', ':scope'));
$qb->setParameter('scope', $params['scope']);
}
if (array_key_exists('types', $params) && null !== $params['types']) {
if (count($params['types']) > 0) {
$qb->andWhere($qb->expr()->in('st.type', ':types'));
$qb->setParameter('types', $params['types']);
}
}
if (array_key_exists('date_status', $params) and !empty($params['date_status'])) {
$this->addTypeFilter($qb, $params);
}
if (array_key_exists('is_closed', $params)) {
$qb->andWhere($this->buildIsClosed($qb, !$params['is_closed']));
}
}
private function buildIsClosed(QueryBuilder $qb, bool $negative = false)
{
if (false === $negative) {
return $qb->expr()->eq('st.closed', "'TRUE'");
}
return $qb->expr()->eq('st.closed', "'FALSE'");
}
private function buildNowIsAfterEndDate(QueryBuilder $qb, $negative = false)
{
if (false === $negative) {
return $qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->lte('st.endDate', ':now'));
}
return $qb->expr()->orX()
->add(
$qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->gt('st.endDate', ':now'))
)
->add($qb->expr()->isNull('st.endDate'));
}
private function buildNowIsAfterStartDate(QueryBuilder $qb, bool $negative = false)
{
if (false === $negative) {
return $qb->expr()->orX()
->add($qb->expr()->lte('st.startDate', ':now'))
->add($qb->expr()->isNull('st.startDate'));
}
return
$qb->expr()->andX()
->add($qb->expr()->gt('st.startDate', ':now'))
->add($qb->expr()->isNotNull('st.startDate'));
}
private function buildNowIsAfterWarningDate(QueryBuilder $qb, bool $negative = false)
{
if (false === $negative) {
return $qb->expr()->andX()
->add(
$qb->expr()->lte(
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
':now'
)
);
}
return $qb->expr()->orX()
->add(
$qb->expr()->andX()
->add($qb->expr()->isNotNull('st.endDate'))
->add($qb->expr()->isNotNull('st.warningInterval'))
->add(
$qb->expr()->gt(
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
':now'
)
)
)
->add($qb->expr()->isNull('st.endDate'))
->add($qb->expr()->isNull('st.warningInterval'));
}
}