瀏覽代碼

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 月之前
父節點
當前提交
b25adac603
共有 1 個文件被更改,包括 14 次插入2 次删除
  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');