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,42 +1,30 @@
<?php
/*
/**
* Chill is a software for social workers
* Copyright (C) 2019 Champs Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Timeline;
use Chill\EventBundle\Entity\Event;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Timeline\TimelineProviderInterface;
use Chill\MainBundle\Timeline\TimelineSingleQuery;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManager;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Doctrine\ORM\Mapping\ClassMetadata;
use LogicException;
use RuntimeException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\Role;
use Doctrine\ORM\Mapping\ClassMetadata;
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Entity\User;
/**
* Class TimelineEventProvider
*
* @package Chill\EventBundle\Timeline
* @author Mathieu Jaumotte jaum_mathieu@collectifs.net
* Class TimelineEventProvider.
*/
class TimelineEventProvider implements TimelineProviderInterface
{
@@ -44,23 +32,19 @@ class TimelineEventProvider implements TimelineProviderInterface
* @var EntityManager
*/
protected $em;
/**
* @var AuthorizationHelper
*/
protected $helper;
/**
* @var User
*/
protected $user;
/**
* TimelineEventProvider constructor.
*
* @param EntityManager $em
* @param AuthorizationHelper $helper
* @param TokenStorageInterface $storage
*/
public function __construct(
EntityManager $em,
@@ -69,44 +53,104 @@ class TimelineEventProvider implements TimelineProviderInterface
) {
$this->em = $em;
$this->helper = $helper;
if (!$storage->getToken()->getUser() instanceof User) {
throw new \RuntimeException('A user should be authenticated !');
throw new RuntimeException('A user should be authenticated !');
}
$this->user = $storage->getToken()->getUser();
}
/**
* @param string $context
* @param array $args
* @return array|string[]
*
* @throws \Doctrine\ORM\Mapping\MappingException
*
* @return array|string[]
*/
public function fetchQuery($context, array $args)
{
$this->checkContext($context);
$metadataEvent = $this->em->getClassMetadata('ChillEventBundle:Event');
$metadataParticipation = $this->em->getClassMetadata('ChillEventBundle:Participation');
$metadataPerson = $this->em->getClassMetadata('ChillPersonBundle:Person');
$query = TimelineSingleQuery::fromArray([
'id' => $metadataEvent->getTableName().'.'.$metadataEvent->getColumnName('id'),
return TimelineSingleQuery::fromArray([
'id' => $metadataEvent->getTableName() . '.' . $metadataEvent->getColumnName('id'),
'type' => 'event',
'date' => $metadataEvent->getTableName().'.'.$metadataEvent->getColumnName('date'),
'date' => $metadataEvent->getTableName() . '.' . $metadataEvent->getColumnName('date'),
'FROM' => $this->getFromClause($metadataEvent, $metadataParticipation, $metadataPerson),
'WHERE' => $this->getWhereClause($metadataEvent, $metadataParticipation, $metadataPerson, $args['person']),
'parameters' => []
'parameters' => [],
]);
return $query;
}
/**
* @return array|mixed[]
*/
public function getEntities(array $ids)
{
$events = $this->em->getRepository(Event::class)
->findBy(['id' => $ids]);
$result = [];
foreach ($events as $event) {
$result[$event->getId()] = $event;
}
return $result;
}
/**
* @param Event $entity
* @param string $context
*
* @return array|mixed[]
*/
public function getEntityTemplate($entity, $context, array $args)
{
$this->checkContext($context);
return [
'template' => 'ChillEventBundle:Timeline:event_person_context.html.twig',
'template_data' => [
'event' => $entity,
'person' => $args['person'],
'user' => $this->user,
],
];
}
/**
* @param string $type
*
* @return bool
*/
public function supportsType($type)
{
return 'event' === $type;
}
/**
* check if the context is supported.
*
* @param string $context
*
* @throws LogicException if the context is not supported
*/
private function checkContext($context)
{
if ('person' !== $context) {
throw new LogicException("The context '{$context}' is not "
. "supported. Currently only 'person' is supported");
}
}
/**
* @param ClassMetadata $metadataEvent
* @param ClassMetadata $metadataParticipation
* @param ClassMetadata $metadataPerson
* @return string
* @throws \Doctrine\ORM\Mapping\MappingException
*
* @return string
*/
private function getFromClause(
ClassMetadata $metadataEvent,
@@ -115,30 +159,25 @@ class TimelineEventProvider implements TimelineProviderInterface
) {
$eventParticipationMapping = $metadataParticipation->getAssociationMapping('event');
$participationPersonMapping = $metadataParticipation->getAssociationMapping('person');
return $metadataEvent->getTableName()
. ' JOIN ' . $metadataParticipation->getTableName()
. ' ON ' . $metadataParticipation->getTableName()
. '.' . $eventParticipationMapping['joinColumns'][0]['name']
. ' = ' . $metadataEvent->getTableName()
. '.' . $eventParticipationMapping['joinColumns'][0]['referencedColumnName']
.' JOIN '.$metadataParticipation->getTableName()
.' ON ' .$metadataParticipation->getTableName()
.'.' .$eventParticipationMapping['joinColumns'][0]['name']
.' = ' .$metadataEvent->getTableName()
.'.' .$eventParticipationMapping['joinColumns'][0]['referencedColumnName']
.' JOIN '.$metadataPerson->getTableName()
.' ON ' .$metadataPerson->getTableName()
.'.' .$participationPersonMapping['joinColumns'][0]['referencedColumnName']
.' = ' .$metadataParticipation->getTableName()
.'.' .$participationPersonMapping['joinColumns'][0]['name']
;
. ' JOIN ' . $metadataPerson->getTableName()
. ' ON ' . $metadataPerson->getTableName()
. '.' . $participationPersonMapping['joinColumns'][0]['referencedColumnName']
. ' = ' . $metadataParticipation->getTableName()
. '.' . $participationPersonMapping['joinColumns'][0]['name'];
}
/**
* @param ClassMetadata $metadataEvent
* @param ClassMetadata $metadataParticipation
* @param ClassMetadata $metadataPerson
* @param Person $person
* @return string
* @throws \Doctrine\ORM\Mapping\MappingException
*
* @return string
*/
private function getWhereClause(
ClassMetadata $metadataEvent,
@@ -147,20 +186,23 @@ class TimelineEventProvider implements TimelineProviderInterface
Person $person
) {
$role = new Role('CHILL_EVENT_SEE');
$reachableCenters = $this->helper->getReachableCenters($this->user, $role);
$associationMapping = $metadataParticipation->getAssociationMapping('person');
if (count($reachableCenters) === 0) {
return 'FALSE = TRUE';
}
$whereClause = sprintf( '%s = %d',
$whereClause = sprintf(
'%s = %d',
$associationMapping['joinColumns'][0]['name'],
$person->getId());
$person->getId()
);
// and
$centerAndScopeLines = array();
$centerAndScopeLines = [];
foreach ($reachableCenters as $center) {
$reachableCircleId = array_map(
function (Scope $scope) { return $scope->getId(); },
@@ -176,68 +218,8 @@ class TimelineEventProvider implements TimelineProviderInterface
implode(',', $reachableCircleId)
);
}
$whereClause .= ' AND ('. implode(' OR ', $centerAndScopeLines) .')';
$whereClause .= ' AND (' . implode(' OR ', $centerAndScopeLines) . ')';
return $whereClause;
}
/**
* check if the context is supported
*
* @param string $context
* @throws \LogicException if the context is not supported
*/
private function checkContext($context)
{
if ($context !== 'person') {
throw new \LogicException("The context '$context' is not "
. "supported. Currently only 'person' is supported");
}
}
/**
* @param string $type
* @return bool
*/
public function supportsType($type)
{
return $type === 'event';
}
/**
* @param array $ids
* @return array|mixed[]
*/
public function getEntities(array $ids) {
$events = $this->em->getRepository(Event::class)
->findBy(array('id' => $ids));
$result = array();
foreach ($events as $event) {
$result[$event->getId()] = $event;
}
return $result;
}
/**
* @param Event $entity
* @param string $context
* @param array $args
* @return array|mixed[]
*/
public function getEntityTemplate($entity, $context, array $args) {
$this->checkContext($context);
return array(
'template' => 'ChillEventBundle:Timeline:event_person_context.html.twig',
'template_data' => array(
'event' => $entity,
'person' => $args['person'],
'user' => $this->user
)
);
}
}