Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[BUGFIX] Refactor stepFechtchingMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
totofff authored and Christophe LETOLLE committed May 22, 2019
1 parent 1506fd7 commit 4499a91
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
11 changes: 11 additions & 0 deletions Classes/Middleware/Item/Step/Service/StepMiddlewareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public function getFirstInvalidStep(Step $step)
return $this->validationService->getFirstInvalidStep($step);
}

/**
* @see \Romm\Formz\Middleware\Item\Step\Service\StepMiddlewareValidationService::getFirstNotValidatedStep
*
* @param StepDefinition $stepDefinition
* @return StepDefinition|null
*/
public function getFirstNotValidatedStep(StepDefinition $stepDefinition)
{
return $this->validationService->getFirstNotValidatedStep($stepDefinition);
}

/**
* @param StepDefinition $step
* @return StepDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ public function stepDefinitionIsValid(StepDefinition $stepDefinition)
&& $stepDefinition->getStep()->getIdentifier() === $this->persistence->getStepIdentifierAtLevel($stepLevel);
}

/**
* Get the first not validated step
*
* @param StepDefinition $stepDefinition
* @return StepDefinition|null
*/
public function getFirstNotValidatedStep(StepDefinition $stepDefinition)
{
if (false === $stepDefinition->hasPreviousDefinition()) {
/*
* No previous step definition found: the user stands on the first
* step, it always has the right to stand there.
*/
return null;
}

$previousStepDefinition = $stepDefinition->getPreviousDefinition();

if ($this->persistence->stepWasValidated($previousStepDefinition->getStep())) {
return $stepDefinition;
}

return $this->getFirstNotValidatedStep($previousStepDefinition);
}

/**
* Searches for the first invalid step among previous steps from the given
* step.
Expand Down Expand Up @@ -186,20 +211,6 @@ public function getFirstInvalidStep(Step $step)
]
);

break;
} else {
$invalidStepDefinition = $stepDefinition;

$this->signalSlotDispatcher->dispatch(
self::class,
self::STEP_INVALID_ACTIVATION,
[
$this->formObject,
$currentStepDefinition,
$invalidStepDefinition
]
);

break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions Classes/Middleware/Item/Step/StepFetchingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public function after(Arguments $arguments)
if ($stepToRedirect instanceof StepDefinition) {
$this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
}

/**
* If we don't find an invalid Step we search the first not validated step
*/
$stepToRedirect = $this->service->getFirstNotValidatedStep($stepDefinition);
if ($stepToRedirect instanceof StepDefinition) {
$this->service->redirectToStep($stepToRedirect->getStep(), $this->redirect());
}
}
}
}
Expand Down

0 comments on commit 4499a91

Please sign in to comment.