Browse Source

Guards time constraint filters from misconfiguration

Adds a flag to track if the time constraint filters have been
configured, preventing errors when attempting to suspend or
restore them before they are initialized. This also corrects
the messaging in the exception thrown when suspension or restoration
is attempted in the wrong state.
Olivier Massot 5 tháng trước cách đây
mục cha
commit
b25adac603

+ 14 - 2
src/Service/Doctrine/FiltersConfigurationService.php

@@ -26,6 +26,8 @@ class FiltersConfigurationService
      */
      */
     protected ?bool $previousTimeConstraintState = null;
     protected ?bool $previousTimeConstraintState = null;
 
 
+    protected bool $filtersConfigured = false;
+
     public function __construct(
     public function __construct(
         private EntityManagerInterface $entityManager,
         private EntityManagerInterface $entityManager,
         private DateTimeConstraint $dateTimeConstraint,
         private DateTimeConstraint $dateTimeConstraint,
@@ -55,6 +57,8 @@ class FiltersConfigurationService
         $activityYearFilter = $this->getFilters()->enable('activity_year_filter');
         $activityYearFilter = $this->getFilters()->enable('activity_year_filter');
         $activityYearFilter->setAccessId($accessId);
         $activityYearFilter->setAccessId($accessId);
         $activityYearFilter->setTimeConstraint($this->activityYearConstraint);
         $activityYearFilter->setTimeConstraint($this->activityYearConstraint);
+
+        $this->filtersConfigured = true;
     }
     }
 
 
     /**
     /**
@@ -65,8 +69,12 @@ class FiltersConfigurationService
      */
      */
     public function suspendTimeConstraintFilters(): void
     public function suspendTimeConstraintFilters(): void
     {
     {
+        if (!$this->filtersConfigured) {
+            return;
+        }
+
         if ($this->previousTimeConstraintState !== null) {
         if ($this->previousTimeConstraintState !== null) {
-            throw new \RuntimeException('time constraints is already suspended');
+            throw new \RuntimeException('The time constraints are already suspended');
         }
         }
 
 
         if ($this->timeFiltersAlreadyDisabled()) {
         if ($this->timeFiltersAlreadyDisabled()) {
@@ -112,8 +120,12 @@ class FiltersConfigurationService
      */
      */
     public function restoreTimeConstraintFilters(): void
     public function restoreTimeConstraintFilters(): void
     {
     {
+        if (!$this->filtersConfigured) {
+            return;
+        }
+
         if ($this->previousTimeConstraintState === null) {
         if ($this->previousTimeConstraintState === null) {
-            throw new \RuntimeException('time constraints has not been suspended, can not be restored');
+            throw new \RuntimeException('The time constraints have not been suspended, can not be restored');
         }
         }
 
 
         $this->enableFilter('date_time_filter');
         $this->enableFilter('date_time_filter');