🎨 check-backup : improve code readability

move exit conditions on top to avoid if/else nightmare
This commit is contained in:
Pierre Goiffon
2018-11-23 17:57:39 +01:00
parent 8530db347b
commit 44d7abac6e

View File

@@ -214,22 +214,39 @@ catch(Exception $e)
$sZipArchiveFile = MakeArchiveFileName().'.tar.gz'; $sZipArchiveFile = MakeArchiveFileName().'.tar.gz';
echo date('Y-m-d H:i:s')." - Checking file: $sZipArchiveFile\n"; echo date('Y-m-d H:i:s')." - Checking file: $sZipArchiveFile\n";
if (file_exists($sZipArchiveFile)) if (!file_exists($sZipArchiveFile))
{
RaiseAlarm("Missing backup file '$sZipArchiveFile'");
return;
}
$aStat = stat($sZipArchiveFile);
if (!$aStat)
{
RaiseAlarm("Failed to stat backup file '$sZipArchiveFile'");
return;
}
$iSize = (int)$aStat['size'];
$iMIN = utils::ReadParam('check_size_min', 0);
if ($iSize <= $iMIN)
{
RaiseAlarm("Backup file '$sZipArchiveFile' too small (Found: $iSize, while expecting $iMIN bytes)");
return;
}
echo "Found the archive\n";
$sOldArchiveFile = MakeArchiveFileName(time() - 86400).'.tar.gz'; // yesterday's archive
if (file_exists($sOldArchiveFile))
{ {
if ($aStat = stat($sZipArchiveFile))
{
$iSize = (int) $aStat['size'];
$iMIN = utils::ReadParam('check_size_min', 0);
if ($iSize > $iMIN)
{
echo "Found the archive\n";
$sOldArchiveFile = MakeArchiveFileName(time() - 86400).'.tar.gz'; // yesterday's archive
if (file_exists($sOldArchiveFile))
{
if ($aOldStat = stat($sOldArchiveFile)) if ($aOldStat = stat($sOldArchiveFile))
{ {
echo "Comparing its size with older file: $sOldArchiveFile\n"; echo "Comparing its size with older file: $sOldArchiveFile\n";
$iOldSize = (int) $aOldStat['size']; $iOldSize = (int)$aOldStat['size'];
$fVariationPercent = 100 * ($iSize - $iOldSize) / $iOldSize; $fVariationPercent = 100 * ($iSize - $iOldSize) / $iOldSize;
$sVariation = round($fVariationPercent, 2)." percent(s)"; $sVariation = round($fVariationPercent, 2)." percent(s)";
@@ -247,21 +264,4 @@ if (file_exists($sZipArchiveFile))
echo "The archive grew by: $sVariation\n"; echo "The archive grew by: $sVariation\n";
} }
} }
}
}
else
{
RaiseAlarm("Backup file '$sZipArchiveFile' too small (Found: $iSize, while expecting $iMIN bytes)");
}
}
else
{
RaiseAlarm("Failed to stat backup file '$sZipArchiveFile'");
}
} }
else
{
RaiseAlarm("Missing backup file '$sZipArchiveFile'");
}
?>