mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 03:23:48 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,33 +1,45 @@
|
||||
<?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\ActivityBundle\Timeline;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||
use Chill\MainBundle\Timeline\TimelineProviderInterface;
|
||||
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
|
||||
use Chill\MainBundle\Timeline\TimelineSingleQuery;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\MainBundle\Timeline\TimelineSingleQuery;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function strtr;
|
||||
|
||||
class TimelineActivityProvider implements TimelineProviderInterface
|
||||
{
|
||||
private const SUPPORTED_CONTEXTS = ['center', 'person'];
|
||||
|
||||
protected ActivityACLAwareRepository $aclAwareRepository;
|
||||
|
||||
protected EntityManagerInterface $em;
|
||||
|
||||
protected AuthorizationHelperInterface $helper;
|
||||
|
||||
protected UserInterface $user;
|
||||
|
||||
protected ActivityACLAwareRepository $aclAwareRepository;
|
||||
|
||||
private const SUPPORTED_CONTEXTS = [ 'center', 'person'];
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
AuthorizationHelperInterface $helper,
|
||||
@@ -38,18 +50,13 @@ class TimelineActivityProvider implements TimelineProviderInterface
|
||||
$this->helper = $helper;
|
||||
$this->aclAwareRepository = $aclAwareRepository;
|
||||
|
||||
if (!$storage->getToken()->getUser() instanceof User)
|
||||
{
|
||||
throw new \RuntimeException('A user should be authenticated !');
|
||||
if (!$storage->getToken()->getUser() instanceof User) {
|
||||
throw new RuntimeException('A user should be authenticated !');
|
||||
}
|
||||
|
||||
$this->user = $storage->getToken()->getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function fetchQuery($context, array $args)
|
||||
{
|
||||
if ('center' === $context) {
|
||||
@@ -59,80 +66,28 @@ class TimelineActivityProvider implements TimelineProviderInterface
|
||||
|
||||
$metadataActivity = $this->em->getClassMetadata(Activity::class);
|
||||
|
||||
[$where, $parameters] = $this->getWhereClauseForPerson($args['person']);
|
||||
[$where, $parameters] = $this->getWhereClauseForPerson($args['person']);
|
||||
|
||||
return TimelineSingleQuery::fromArray([
|
||||
'id' => $metadataActivity->getTableName()
|
||||
.'.'.$metadataActivity->getColumnName('id'),
|
||||
'type' => 'activity',
|
||||
'date' => $metadataActivity->getTableName()
|
||||
.'.'.$metadataActivity->getColumnName('date'),
|
||||
'FROM' => $this->getFromClausePerson(),
|
||||
'WHERE' => $where,
|
||||
'parameters' => $parameters
|
||||
]);
|
||||
}
|
||||
|
||||
private function getWhereClauseForPerson(Person $person)
|
||||
{
|
||||
$parameters = [];
|
||||
$metadataActivity = $this->em->getClassMetadata(Activity::class);
|
||||
$associationMapping = $metadataActivity->getAssociationMapping('person');
|
||||
$role = new Role('CHILL_ACTIVITY_SEE');
|
||||
$reachableScopes = $this->helper->getReachableScopes($this->user, $role->getRole(), $person->getCenter());
|
||||
$whereClause = sprintf(' {activity.person_id} = ? AND {activity.scope_id} IN ({scopes_ids}) ');
|
||||
$scopes_ids = [];
|
||||
|
||||
// first parameter: activity.person_id
|
||||
$parameters[] = $person->getId();
|
||||
|
||||
// loop on reachable scopes
|
||||
foreach ($reachableScopes as $scope) {
|
||||
if (\in_array($scope->getId(), $scopes_ids)) {
|
||||
continue;
|
||||
}
|
||||
$scopes_ids[] = '?';
|
||||
$parameters[] = $scope->getId();
|
||||
}
|
||||
|
||||
return [
|
||||
\strtr(
|
||||
$whereClause,
|
||||
[
|
||||
'{activity.person_id}' => $associationMapping['joinColumns'][0]['name'],
|
||||
'{activity.scope_id}' => $metadataActivity->getTableName().'.'.
|
||||
$metadataActivity->getAssociationMapping('scope')['joinColumns'][0]['name'],
|
||||
'{scopes_ids}' => \implode(", ", $scopes_ids)
|
||||
,
|
||||
]
|
||||
),
|
||||
$parameters
|
||||
];
|
||||
}
|
||||
|
||||
private function getFromClausePerson(): string
|
||||
{
|
||||
$metadataActivity = $this->em->getClassMetadata(Activity::class);
|
||||
$metadataPerson = $this->em->getClassMetadata(Person::class);
|
||||
$associationMapping = $metadataActivity->getAssociationMapping('person');
|
||||
|
||||
return sprintf(
|
||||
"%s JOIN %s ON %s.%s = %s",
|
||||
$metadataActivity->getTableName(),
|
||||
$metadataPerson->getTableName(),
|
||||
$metadataPerson->getTableName(),
|
||||
$associationMapping['joinColumns'][0]['referencedColumnName'],
|
||||
$associationMapping['joinColumns'][0]['name']
|
||||
);
|
||||
'id' => $metadataActivity->getTableName()
|
||||
. '.' . $metadataActivity->getColumnName('id'),
|
||||
'type' => 'activity',
|
||||
'date' => $metadataActivity->getTableName()
|
||||
. '.' . $metadataActivity->getColumnName('date'),
|
||||
'FROM' => $this->getFromClausePerson(),
|
||||
'WHERE' => $where,
|
||||
'parameters' => $parameters,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getEntities(array $ids): array
|
||||
{
|
||||
$activities = $this->em->getRepository(Activity::class)
|
||||
->findBy(array('id' => $ids));
|
||||
->findBy(['id' => $ids]);
|
||||
|
||||
$result = array();
|
||||
foreach($activities as $activity) {
|
||||
$result = [];
|
||||
|
||||
foreach ($activities as $activity) {
|
||||
$result[$activity->getId()] = $activity;
|
||||
}
|
||||
|
||||
@@ -144,28 +99,28 @@ class TimelineActivityProvider implements TimelineProviderInterface
|
||||
$this->checkContext($context);
|
||||
|
||||
return [
|
||||
'template' => 'ChillActivityBundle:Timeline:activity_person_context.html.twig',
|
||||
'template_data' => [
|
||||
'activity' => $entity,
|
||||
'context' => $context
|
||||
]
|
||||
'template' => 'ChillActivityBundle:Timeline:activity_person_context.html.twig',
|
||||
'template_data' => [
|
||||
'activity' => $entity,
|
||||
'context' => $context,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsType($type): bool
|
||||
{
|
||||
return $type === 'activity';
|
||||
return 'activity' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the context is supported.
|
||||
*
|
||||
* @throws \LogicException if the context is not supported
|
||||
* @throws LogicException if the context is not supported
|
||||
*/
|
||||
private function checkContext(string $context)
|
||||
{
|
||||
if (FALSE === \in_array($context, self::SUPPORTED_CONTEXTS)) {
|
||||
throw new \LogicException(
|
||||
if (false === in_array($context, self::SUPPORTED_CONTEXTS)) {
|
||||
throw new LogicException(
|
||||
sprintf(
|
||||
"The context '%s' is not supported. Currently only 'person' is supported",
|
||||
$context
|
||||
@@ -174,4 +129,55 @@ class TimelineActivityProvider implements TimelineProviderInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function getFromClausePerson(): string
|
||||
{
|
||||
$metadataActivity = $this->em->getClassMetadata(Activity::class);
|
||||
$metadataPerson = $this->em->getClassMetadata(Person::class);
|
||||
$associationMapping = $metadataActivity->getAssociationMapping('person');
|
||||
|
||||
return sprintf(
|
||||
'%s JOIN %s ON %s.%s = %s',
|
||||
$metadataActivity->getTableName(),
|
||||
$metadataPerson->getTableName(),
|
||||
$metadataPerson->getTableName(),
|
||||
$associationMapping['joinColumns'][0]['referencedColumnName'],
|
||||
$associationMapping['joinColumns'][0]['name']
|
||||
);
|
||||
}
|
||||
|
||||
private function getWhereClauseForPerson(Person $person)
|
||||
{
|
||||
$parameters = [];
|
||||
$metadataActivity = $this->em->getClassMetadata(Activity::class);
|
||||
$associationMapping = $metadataActivity->getAssociationMapping('person');
|
||||
$role = new Role('CHILL_ACTIVITY_SEE');
|
||||
$reachableScopes = $this->helper->getReachableScopes($this->user, $role->getRole(), $person->getCenter());
|
||||
$whereClause = sprintf(' {activity.person_id} = ? AND {activity.scope_id} IN ({scopes_ids}) ');
|
||||
$scopes_ids = [];
|
||||
|
||||
// first parameter: activity.person_id
|
||||
$parameters[] = $person->getId();
|
||||
|
||||
// loop on reachable scopes
|
||||
foreach ($reachableScopes as $scope) {
|
||||
if (in_array($scope->getId(), $scopes_ids)) {
|
||||
continue;
|
||||
}
|
||||
$scopes_ids[] = '?';
|
||||
$parameters[] = $scope->getId();
|
||||
}
|
||||
|
||||
return [
|
||||
strtr(
|
||||
$whereClause,
|
||||
[
|
||||
'{activity.person_id}' => $associationMapping['joinColumns'][0]['name'],
|
||||
'{activity.scope_id}' => $metadataActivity->getTableName() . '.' .
|
||||
$metadataActivity->getAssociationMapping('scope')['joinColumns'][0]['name'],
|
||||
'{scopes_ids}' => implode(', ', $scopes_ids),
|
||||
]
|
||||
),
|
||||
$parameters,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user