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 months ago
parent
commit
b25adac603
1 changed files with 14 additions and 2 deletions
  1. 14 2
      src/Service/Doctrine/FiltersConfigurationService.php

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

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