diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index 7599321a..0b063ae4 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -9,6 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface; use App\Service\InsuranceConnector; +use App\Insurance\ClientData; + class TestInsuranceConnectorCommand extends Command { protected $insurance; @@ -29,43 +31,42 @@ class TestInsuranceConnectorCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - $client_info = [ - 'client_type' => 'I', - 'first_name' => '', - 'middle_name' => 'Resq', - 'surname' => 'Customer', - 'corporate_name' => 'Customer, Test Resq' - ]; + $client_data = new ClientData(); - $client_contact_info = [ - 'address_number' => '112', - 'address_street' => 'Test Street', - 'address_building' => 'Test Building', - 'address_barangay' => 'Legaspi Village', - 'address_city' => 'Makati City', - 'address_province' => 'Metro Manila', - 'zipcode' => '1200', - 'mobile_number' => '09171234567', - 'email_address' => 'test.resq@gmail.com' - ]; + // set client info part + $client_data->setClientType('I') + ->setFirstName('Test') + ->setMiddleName('Resq') + ->setSurname('Customer') + ->setCorporateName('Customer, Test Resq'); + + // set the client contact info part + $client_data->setAddressNumber('113') + ->setAddressStreet('Test Street') + ->setAddressBuilding('Test Building') + ->setAddressBarangay('Legaspi Village') + ->setAddressCity('Makati City') + ->setAddressProvince('Metro Manila') + ->setZipcode('1200') + ->setMobileNumber('09171234567') + ->setEmailAddress('test.resq@gmail.com'); - $car_info = [ - 'make' => 'HYUNDAI', - 'model' => 'ACCENT', - 'series' => '1.4 A/T', - 'color' => 'PHANTOM BLACK', - 'plate_number' => 'ABC1234', - 'mv_file_number' => '123456789012345', - 'motor_number' => 'E31TE-0075268', - 'serial_chasis' => 'PA0SEF210K0075701', - 'year_model' => '2020', - 'mv_type_id' => 1, - 'body_type' => 'SEDAN', - 'is_public' => false, - 'line' => 'pcoc' - ]; + // set the car info part + $client_data->setMake('HYUNDAI') + ->setModel('ACCENT') + ->setSeries('1.4 A/T') + ->setColor('PHANTOM BLACK') + ->setPlateNumber('ABC1234') + ->setMvFileNumber('123456789012345') + ->setMotorNumber('E31TE-0075268') + ->setSerialChassis('PA0SEF210K0075701') + ->setYearModel('2020') + ->setMvTypeID(1) + ->setBodyType('SEDAN') + ->setLine('pcoc') + ->setPublic(false); - $result = $this->insurance->createApplication($client_info, $client_contact_info, $car_info); + $result = $this->insurance->processApplication($client_data); error_log(json_encode($result)); diff --git a/src/Insurance/ClientData.php b/src/Insurance/ClientData.php index 68113f32..9707eadf 100644 --- a/src/Insurance/ClientData.php +++ b/src/Insurance/ClientData.php @@ -147,13 +147,13 @@ class ClientData return $this->address_street; } - public function setAdddressBuilding($address_building) + public function setAddressBuilding($address_building) { $this->address_building = $address_building; return $this; } - public function getAdddressBuilding() + public function getAddressBuilding() { return $this->address_building; } diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php index fbf306f6..a58bd50f 100644 --- a/src/Service/InsuranceConnector.php +++ b/src/Service/InsuranceConnector.php @@ -24,7 +24,7 @@ class InsuranceConnector $this->token = $token; } - public function createClientData(ClientData $client_data) + public function processApplication(ClientData $client_data) { // transform ClientData into the format needed for insurance application $client_info = [ @@ -43,13 +43,13 @@ class InsuranceConnector 'address_city' => $client_data->getAddressCity(), 'address_province' => $client_data->getAddressProvince(), 'zipcode' => $client_data->getZipcode(), - 'mobile_number' => $cilent_data->getMobileNumber(), + 'mobile_number' => $client_data->getMobileNumber(), 'email_address' => $client_data->getEmailAddress(), ]; $car_info = [ 'make' => $client_data->getMake(), - 'model' => $client_data->getModel(),, + 'model' => $client_data->getModel(), 'series' => $client_data->getSeries(), 'color' => $client_data->getColor(), 'plate_number' => $client_data->getPlateNumber(), @@ -69,12 +69,17 @@ class InsuranceConnector 'car_info' => $car_info, ]; - $result = $this->createApplication($app_data); + $result = $this->sendApplication($app_data); - // TODO: error handling + $app_result = json_decode($result, true); + + // process response received from insurance + $res = $this->checkApplicationResult($app_result); + + return $res; } - protected function createApplication($body) + protected function sendApplication($body) { $body_text = json_encode($body); @@ -83,13 +88,45 @@ class InsuranceConnector $res = $this->curlPost('api/v1/ctpl/applications', $body_text, $token); - $app_res = json_decode($res, true); + return $res; + } - // check result - if ($app_res == null) + protected function checkApplicationResult($app_result) + { + if ($app_result == null) + { + // insurance api returned an empty json return []; + } - return $app_res; + // check if message is set + $message = ''; + if (isset($app_result['message'])) + $message = $app_result['message']; + + // check if id is set + $id = ''; + if (isset($app_result['id'])) + $id = $app_result['id']; + + // check if status is set, meaning, there's an error + $status = ''; + if (isset($app_result['status'])) + $status = 'error'; + else + $status = 'success'; + + $data = [ + 'id' => $id + ]; + + $processed_result = [ + 'status' => $status, + 'message' => $message, + 'data' => $data, + ]; + + return $processed_result; } protected function generateAuthToken($username, $password)