From 9efe73e8b246400b825a9206481c0e6f5a808e24 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 21 Sep 2022 04:54:21 +0000 Subject: [PATCH] Add validation for invalid number of fields. #704 --- src/Command/LoadWarrantySerialCommand.php | 32 ++++++++++++++++------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Command/LoadWarrantySerialCommand.php b/src/Command/LoadWarrantySerialCommand.php index 3ef4d849..e9fc6718 100644 --- a/src/Command/LoadWarrantySerialCommand.php +++ b/src/Command/LoadWarrantySerialCommand.php @@ -23,6 +23,8 @@ use DateTime; class LoadWarrantySerialCommand extends Command { + const FIELD_COUNT = 7; + protected $em; protected $upload_logger; protected $load_logger; @@ -214,13 +216,11 @@ class LoadWarrantySerialCommand extends Command // (5) Empty line - ignore // (6) empty sku - log - // TODO: count the number of fields - - // check if the line is a header - if ($row[0] == 'SerialNumber') + // check if empty line + if ($row == array(null)) { // no need to log, but send back error - $error = 'Invalid information.'; + $error = 'Empty line'; $data = [ 'serial' => '', 'status' => 'error', @@ -231,11 +231,25 @@ class LoadWarrantySerialCommand extends Command return $data; } - // check if empty line - if ($row == array(null)) + // check the number of fields + 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 - $error = 'Empty line'; + $error = 'Invalid information.'; $data = [ 'serial' => '', 'status' => 'error', @@ -412,7 +426,7 @@ class LoadWarrantySerialCommand extends Command curl_close($curl); // check result - // error_log('Result ' . $res); + error_log('Result ' . $res); } }