mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2026-01-05 04:15:23 +01:00
Added some error checking on runSpeedtest()
This commit is contained in:
@@ -25,6 +25,11 @@ class SpeedtestHelper {
|
||||
|
||||
try {
|
||||
$output = json_decode($output, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
if(!SpeedtestHelper::checkOutputIsComplete($output)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$test = Speedtest::create([
|
||||
'ping' => $output['ping']['latency'],
|
||||
'download' => SpeedtestHelper::convert($output['download']['bandwidth']),
|
||||
@@ -142,4 +147,46 @@ class SpeedtestHelper {
|
||||
'unit' => 'Mbit/s'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the speedtest JSON output is complete/valid
|
||||
*
|
||||
* @param array $output
|
||||
* @return boolean
|
||||
*/
|
||||
public static function checkOutputIsComplete($output)
|
||||
{
|
||||
/**
|
||||
* Array of indexes that must exist in $output
|
||||
*/
|
||||
$checks = [
|
||||
'type' => 'result',
|
||||
'download' => [ 'bandwidth' => '*' ],
|
||||
'upload' => [ 'bandwidth' => '*' ],
|
||||
'ping' => [ 'latency' => '*' ],
|
||||
'server' => [
|
||||
'id' => '*',
|
||||
'name' => '*',
|
||||
'host' => '*',
|
||||
'port' => '*'
|
||||
],
|
||||
'result' => [
|
||||
'url' => '*'
|
||||
],
|
||||
];
|
||||
/**
|
||||
* Array of indexes that must not exist
|
||||
*/
|
||||
$checkMissing = [
|
||||
'type' => 'error'
|
||||
];
|
||||
|
||||
foreach($checks as $key => $value) {
|
||||
if(!isset($output[$key])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user