Add validation for invalid number of fields. #704

This commit is contained in:
Korina Cordero 2022-09-21 04:54:21 +00:00
parent f40155e1f5
commit 9efe73e8b2

View file

@ -23,6 +23,8 @@ use DateTime;
class LoadWarrantySerialCommand extends Command class LoadWarrantySerialCommand extends Command
{ {
const FIELD_COUNT = 7;
protected $em; protected $em;
protected $upload_logger; protected $upload_logger;
protected $load_logger; protected $load_logger;
@ -214,13 +216,11 @@ class LoadWarrantySerialCommand extends Command
// (5) Empty line - ignore // (5) Empty line - ignore
// (6) empty sku - log // (6) empty sku - log
// TODO: count the number of fields // check if empty line
if ($row == array(null))
// check if the line is a header
if ($row[0] == 'SerialNumber')
{ {
// no need to log, but send back error // no need to log, but send back error
$error = 'Invalid information.'; $error = 'Empty line';
$data = [ $data = [
'serial' => '', 'serial' => '',
'status' => 'error', 'status' => 'error',
@ -231,11 +231,25 @@ class LoadWarrantySerialCommand extends Command
return $data; return $data;
} }
// check if empty line // check the number of fields
if ($row == array(null)) if (count($row) != self::FIELD_COUNT)
{
$error = 'Invalid number of fields.';
$data = [
'serial' => '',
'status' => 'error',
'has_error' => true,
'error_message' => $error,
];
return $data;
}
// check if the line is a header
if ($row[0] == 'SerialNumber')
{ {
// no need to log, but send back error // no need to log, but send back error
$error = 'Empty line'; $error = 'Invalid information.';
$data = [ $data = [
'serial' => '', 'serial' => '',
'status' => 'error', 'status' => 'error',
@ -412,7 +426,7 @@ class LoadWarrantySerialCommand extends Command
curl_close($curl); curl_close($curl);
// check result // check result
// error_log('Result ' . $res); error_log('Result ' . $res);
} }
} }