|
|
@@ -3,19 +3,24 @@
|
|
|
namespace App\Filter\Doctrine\TimeConstraint;
|
|
|
|
|
|
use App\Entity\Access\Access;
|
|
|
+use App\Service\Constraint\AbstractTimeConstraintUtils;
|
|
|
use App\Service\Constraint\ActivityYearConstraint;
|
|
|
use App\Service\Constraint\DateTimeConstraint;
|
|
|
+use App\Service\Constraint\TimeConstraintInterface;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Doctrine\ORM\Query\Filter\SQLFilter;
|
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
+use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
+use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
|
|
/**
|
|
|
* Applique les contraintes temporelles aux entités possédant l'annotation requise
|
|
|
*/
|
|
|
abstract class AbstractTimeFilter extends SQLFilter
|
|
|
{
|
|
|
- protected ?Access $access = null;
|
|
|
+ protected ?int $accessId = null;
|
|
|
+ protected ?TimeConstraintInterface $timeConstraint = null;
|
|
|
|
|
|
/**
|
|
|
* Annotation expected, have to be re-defined in subclasses
|
|
|
@@ -23,24 +28,70 @@ abstract class AbstractTimeFilter extends SQLFilter
|
|
|
*/
|
|
|
protected static ?string $constraintAnnotation = null;
|
|
|
|
|
|
+ /**
|
|
|
+ * Parameter of the annotation that design the field containing the starting date of the period
|
|
|
+ * Have to be re-defined in subclasses
|
|
|
+ * @var string|null
|
|
|
+ */
|
|
|
+ protected static ?string $annotationStartField = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Parameter of the annotation that design the field containing the ending date of the period
|
|
|
+ * Have to be re-defined in subclasses
|
|
|
+ * @var string|null
|
|
|
+ */
|
|
|
+ protected static ?string $annotationEndField = null;
|
|
|
+
|
|
|
// <--- Dependency injections
|
|
|
protected EntityManagerInterface $entityManager;
|
|
|
- protected Security $security;
|
|
|
- protected RequestStack $requestStack;
|
|
|
-
|
|
|
- public function setEntityManagerInterface(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager;}
|
|
|
- public function setSecurity(Security $security) { $this->security = $security;}
|
|
|
- public function setRequestStack(RequestStack $requestStack) { $this->requestStack = $requestStack;}
|
|
|
|
|
|
+ #[Required]
|
|
|
+ public function setEntityManagerInterface(EntityManagerInterface $entityManager): void { $this->entityManager = $entityManager;}
|
|
|
// --->
|
|
|
|
|
|
+ public function setAccessId(int $accessId): void {
|
|
|
+ $this->accessId = $accessId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setTimeConstraint(TimeConstraintInterface $timeConstraint): void {
|
|
|
+ $this->timeConstraint = $timeConstraint;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retourne les noms des champs contenant les dates de début et de fin de la période de la ressource testée.
|
|
|
+ *
|
|
|
+ * @param array<string, string> $arguments
|
|
|
+ * @return array<string>
|
|
|
+ */
|
|
|
+ protected function getStartAndEndFields(array $arguments): array {
|
|
|
+ if (static::$annotationStartField === null || static::$annotationEndField) {
|
|
|
+ throw new \RuntimeException('Constraint annotation has not been properly configured');
|
|
|
+ }
|
|
|
+
|
|
|
+ $startField = $arguments[static::$annotationStartField] ?? null;
|
|
|
+ $endField = $arguments[static::$annotationEndField] ?? null;
|
|
|
+
|
|
|
+ return [$startField, $endField];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Invoke the TimeConstraintUtils to retrieve the active constraints for the given user
|
|
|
+ *
|
|
|
+ * @param int $accessId
|
|
|
+ * @return array<string, array<string, list<int>>>
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ protected function getConstraints(int $accessId): array
|
|
|
+ {
|
|
|
+ return $this->timeConstraint->invoke($accessId);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Méthode surchargée de SQLFilter permettant d'appliquer un filtre supplémentaire aux requêtes SQL
|
|
|
*
|
|
|
* @param ClassMetadata $targetEntity
|
|
|
* @param string $targetTableAlias
|
|
|
* @return string
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
|
|
|
{
|
|
|
@@ -48,27 +99,17 @@ abstract class AbstractTimeFilter extends SQLFilter
|
|
|
throw new \RuntimeException('Constraint annotation has not been set');
|
|
|
}
|
|
|
|
|
|
- $access = $this->security->getUser();
|
|
|
- if (!$access) {
|
|
|
- return '';
|
|
|
- }
|
|
|
-
|
|
|
- if (!$this->areTimeConstraintsEnabled()) {
|
|
|
- return '';
|
|
|
- }
|
|
|
-
|
|
|
$constraintAnnotation = $targetEntity->getReflectionClass()->getAttributes(static::$constraintAnnotation)[0] ?? null;
|
|
|
if (!$constraintAnnotation) {
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
- $startFieldName = $constraintAnnotation->getArguments()['startYearFieldName'] ?? null;
|
|
|
- $endFieldName = $constraintAnnotation->getArguments()['endYearFieldName'] ?? null;
|
|
|
+ [$startFieldName, $endFieldName] = $this->getStartAndEndFields($constraintAnnotation->getArguments());
|
|
|
if (!$startFieldName || !$endFieldName) {
|
|
|
throw new \RuntimeException('Missing start and/or end field names in constraint annotation');
|
|
|
}
|
|
|
|
|
|
- $constraints = $this->getConstraints($this->access->getId());
|
|
|
+ $constraints = $this->getConstraints($this->accessId);
|
|
|
|
|
|
$fields = [
|
|
|
ActivityYearConstraint::START_KEY => $startFieldName,
|
|
|
@@ -78,12 +119,6 @@ abstract class AbstractTimeFilter extends SQLFilter
|
|
|
return $this->constructQuery($constraints, $targetTableAlias, $fields);
|
|
|
}
|
|
|
|
|
|
- protected function areTimeConstraintsEnabled(): bool {
|
|
|
- return $this->requestStack->getMainRequest()->get('_time_constraint', true);
|
|
|
- }
|
|
|
-
|
|
|
- abstract protected function getConstraints(int $accessId): array;
|
|
|
-
|
|
|
/**
|
|
|
* Fonction permettant de construire la requête SQL correspondante aux contraintes
|
|
|
*
|
|
|
@@ -117,6 +152,7 @@ abstract class AbstractTimeFilter extends SQLFilter
|
|
|
|
|
|
/**
|
|
|
* Fonction retournant la valeur arithmétique correspondant à la condition de la contrainte
|
|
|
+ *
|
|
|
* @param int $condition
|
|
|
* @return string|null
|
|
|
* @see DateTimeFilterTest::testGetArithmeticValue()
|