Skip to content

Commit

Permalink
Removing failing state machines when dealing with multiple states.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell committed May 29, 2019
1 parent bbee4f7 commit c6ede30
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/StateMachine/MachineOfMachines.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public function addMachine($state, IStateMachine $machine)
public function processInput(string $input)
{
$got_machine = false;
$machine_error = false;
$machine_finished = false;

$machines = [];
$errors = [];

foreach ($this->currentStates as $current_state) {
foreach ($this->currentStates as $key => $current_state) {
$machine = $this->getStateMachine($current_state);
if ($machine) {
$machines[] = $machine;
Expand All @@ -29,9 +28,13 @@ public function processInput(string $input)
$machine->processInput($input);
} catch (\Exception $e) {
$errors[] = true;
// The current machine could not handle the input... transition.

if ($machine->isCurrentlyAtAnEndState()) {
$machine_finished = true;
} else {
if (count($this->currentStates) > 1) {
unset($this->currentStates[$key]);
}
}
$machine->reset();
}
Expand All @@ -43,7 +46,8 @@ public function processInput(string $input)
if ($machine_finished) {
parent::processInput($input);
} else {
throw new \Exception("We had machines for state(s) " . implode(", ", $this->currentStates) . " but no machine finished");
throw new \Exception("We had machines for state(s) " .
implode(", ", $this->currentStates) . " but no machine finished");
}
}
} else {
Expand Down

0 comments on commit c6ede30

Please sign in to comment.