Skip to content

Commit 344d8aa

Browse files
committed
Convert if to switch
1 parent d3cac64 commit 344d8aa

1 file changed

Lines changed: 67 additions & 65 deletions

File tree

library/Vspheredb/Daemon/VsphereDbDaemon.php

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -168,77 +168,79 @@ protected function onComponentChange($component, $formerState, $currentState)
168168
$this->daemonState->getComponentState($component)
169169
));
170170
}
171-
if ($component === self::COMPONENT_DB) {
172-
if ($formerState === self::STATE_READY) {
173-
$this->stopConfigWatch();
174-
$this->stopComponent(self::COMPONENT_API);
175-
$this->stopComponent(self::COMPONENT_LOCALDB);
176-
} elseif ($currentState === self::STATE_IDLE) {
177-
$this->runConfigWatch();
178-
} elseif ($currentState === self::STATE_READY) {
179-
$this->setLocalDbState(self::STATE_STARTING);
180-
}
181-
if ($currentState === self::STATE_FAILED) {
182-
$this->loop->addTimer(10, function () {
183-
$this->stopDbProcess();
171+
switch ($component) {
172+
case self::COMPONENT_DB:
173+
if ($formerState === self::STATE_READY) {
184174
$this->stopConfigWatch();
185-
if ($this->daemonState->getComponentState(self::COMPONENT_DB) === self::STATE_FAILED) {
186-
$this->initializeDbProcess();
187-
}
188-
});
189-
}
190-
}
191-
if ($component === self::COMPONENT_LOCALDB) {
192-
if ($formerState === self::STATE_READY) {
193-
$this->stopComponent(self::COMPONENT_API);
194-
}
195-
switch ($currentState) {
196-
case self::STATE_STARTING:
197-
$this->reconnectToDb();
198-
break;
199-
case self::STATE_FAILED:
200-
$this->logger->error('Failed. Will try to reconnect to the Database');
201-
$this->eventuallyDisconnectFromDb();
202-
$delay = $this->delayOnFailed;
203-
$this->logger->warning("Failed. Reconnecting in {$delay}s");
204-
$this->loop->addTimer($delay, function () {
205-
if ($this->getLocalDbState() === self::STATE_FAILED) {
206-
$this->setLocalDbState(self::STATE_STARTING);
175+
$this->stopComponent(self::COMPONENT_API);
176+
$this->stopComponent(self::COMPONENT_LOCALDB);
177+
} elseif ($currentState === self::STATE_IDLE) {
178+
$this->runConfigWatch();
179+
} elseif ($currentState === self::STATE_READY) {
180+
$this->setLocalDbState(self::STATE_STARTING);
181+
}
182+
if ($currentState === self::STATE_FAILED) {
183+
$this->loop->addTimer(10, function () {
184+
$this->stopDbProcess();
185+
$this->stopConfigWatch();
186+
if ($this->daemonState->getComponentState(self::COMPONENT_DB) === self::STATE_FAILED) {
187+
$this->initializeDbProcess();
207188
}
208189
});
209-
break;
210-
case self::STATE_READY:
211-
$this->setApiState(self::STATE_STARTING);
212-
break;
213-
case self::STATE_STOPPING:
214-
$this->eventuallyDisconnectFromDb();
190+
}
191+
break;
192+
case self::COMPONENT_LOCALDB:
193+
if ($formerState === self::STATE_READY) {
215194
$this->stopComponent(self::COMPONENT_API);
216-
$this->setLocalDbState(self::STATE_STOPPED);
217-
break;
218-
}
219-
}
220-
if ($component === self::COMPONENT_API) {
221-
switch ($currentState) {
222-
case self::STATE_STARTING:
223-
$this->apiConnectionHandler->run($this->loop);
224-
$this->setApiState(self::STATE_READY);
225-
break;
226-
case self::STATE_READY:
227-
$this->refreshConfiguredServers();
228-
$this->daemonState->setState(self::STATE_READY);
229-
break;
230-
case self::STATE_FAILED:
231-
$this->logger->error('[api] failed');
195+
}
196+
switch ($currentState) {
197+
case self::STATE_STARTING:
198+
$this->reconnectToDb();
199+
break;
200+
case self::STATE_FAILED:
201+
$this->logger->error('Failed. Will try to reconnect to the Database');
202+
$this->eventuallyDisconnectFromDb();
203+
$delay = $this->delayOnFailed;
204+
$this->logger->warning("Failed. Reconnecting in {$delay}s");
205+
$this->loop->addTimer($delay, function () {
206+
if ($this->getLocalDbState() === self::STATE_FAILED) {
207+
$this->setLocalDbState(self::STATE_STARTING);
208+
}
209+
});
210+
break;
211+
case self::STATE_READY:
212+
$this->setApiState(self::STATE_STARTING);
213+
break;
214+
case self::STATE_STOPPING:
215+
$this->eventuallyDisconnectFromDb();
216+
$this->stopComponent(self::COMPONENT_API);
217+
$this->setLocalDbState(self::STATE_STOPPED);
218+
break;
219+
}
220+
break;
221+
case self::COMPONENT_API:
222+
switch ($currentState) {
223+
case self::STATE_STARTING:
224+
$this->apiConnectionHandler->run($this->loop);
225+
$this->setApiState(self::STATE_READY);
226+
break;
227+
case self::STATE_READY:
228+
$this->refreshConfiguredServers();
229+
$this->daemonState->setState(self::STATE_READY);
230+
break;
231+
case self::STATE_FAILED:
232+
$this->logger->error('[api] failed');
232233
// Intentional fall-through:
233234
// no break
234-
case self::STATE_STOPPING:
235-
if ($this->apiConnectionHandler) {
236-
$this->apiConnectionHandler->stop();
237-
}
238-
$this->stopAllApiTasks();
239-
$this->setApiState(self::STATE_STOPPED);
240-
break;
241-
}
235+
case self::STATE_STOPPING:
236+
if ($this->apiConnectionHandler) {
237+
$this->apiConnectionHandler->stop();
238+
}
239+
$this->stopAllApiTasks();
240+
$this->setApiState(self::STATE_STOPPED);
241+
break;
242+
}
243+
break;
242244
}
243245
}
244246

0 commit comments

Comments
 (0)