public function validate($target, \FS\Components\Notifier $notifier)
 {
     // if user set/update token, we need to use the latest entered one
     if (isset($target['token'])) {
         $this->getApplicationContext()->getComponent('\\FS\\Components\\Http\\Client')->setToken($target['token']);
     }
     // enabled?
     if ($target['enabled'] != 'yes') {
         $notifier->warning(__('FlagShip Shipping is disabled.', FLAGSHIP_SHIPPING_TEXT_DOMAIN));
     }
     // phone
     $phoneValidator = new PhoneValidator();
     $phoneValidator->validate($target['shipper_phone_number'], $notifier);
     // address
     $addressValidator = $this->getApplicationContext()->getComponent('\\FS\\Components\\Validation\\AddressValidator');
     $address = $addressValidator->validate(array('postal_code' => $target['origin'], 'state' => $target['freight_shipper_state'], 'city' => $target['freight_shipper_city'], 'country' => 'CA'), $notifier);
     $target['origin'] = $address['postal_code'];
     $target['freight_shipper_state'] = $address['state'];
     $target['freight_shipper_city'] = $address['city'];
     // credentials
     if (!$target['shipper_person_name']) {
         $notifier->warning(__('Shipper person name is missing.', FLAGSHIP_SHIPPING_TEXT_DOMAIN));
     }
     if (!$target['shipper_company_name']) {
         $notifier->warning(__('Shipper company name is missing.', FLAGSHIP_SHIPPING_TEXT_DOMAIN));
     }
     if (!$target['shipper_phone_number']) {
         $notifier->warning(__('Shipper phone number is missing.', FLAGSHIP_SHIPPING_TEXT_DOMAIN));
     }
     if (!$target['freight_shipper_street']) {
         $notifier->warning(__('Shipper address\'s streetline is missing.', FLAGSHIP_SHIPPING_TEXT_DOMAIN));
     }
     // overall integrity, send mock quote request
     $integrityValidator = $this->getApplicationContext()->getComponent('\\FS\\Components\\Validation\\SettingsIntegrityValidator');
     $integrityValidator->validate($target, $notifier);
     return $target;
 }
 public function parsePhoneNumbers($domain_id)
 {
     $phone_number = array();
     $email_patterns = EmailDomainValidator::where("domain_id", "=", $domain_id)->orderBy('status', 'desc')->get()->toArray();
     foreach ($email_patterns as $email_pattern) {
         // Handle Netprospex Person Match API Call Response
         if ($email_pattern['net_status'] == 1) {
             $netprospex_details = json_decode($email_pattern['netprospex_match']);
             foreach ($netprospex_details as $netprospex_detail) {
                 $person = $netprospex_detail->person_profile->person;
                 if (isset($person->phones)) {
                     // Check Person Phone Numbers
                     $netprospex_user_phones = $person->phones;
                     foreach ($netprospex_user_phones as $netprospex_user_phone) {
                         if ($netprospex_user_phone->label == 'directPhone') {
                             $netprospex_user_phone->formattedNumber = $this->filterPhoneNumber($netprospex_user_phone->formattedNumber);
                             $phone_record = array('domain_id' => $domain_id, 'phone' => $netprospex_user_phone->formattedNumber, 'type' => 'direct', 'is_current' => 0);
                             array_push($phone_number, $phone_record);
                         }
                     }
                 }
                 if (isset($person->organization->phones)) {
                     // Check Organization Phone Numbers
                     $netprospex_organization_phones = $person->organization->phones;
                     foreach ($netprospex_organization_phones as $netprospex_organization_phone) {
                         $netprospex_organization_phone->formattedNumber = $this->filterPhoneNumber($netprospex_organization_phone->formattedNumber);
                         $phone_record = array('domain_id' => $domain_id, 'phone' => $netprospex_organization_phone->formattedNumber, 'type' => 'company', 'is_current' => 0);
                         array_push($phone_number, $phone_record);
                     }
                 }
             }
         }
         // Handle Zoominfo Person Detail API Call Response
         if ($email_pattern['zoom_status'] == 1) {
             $zoominfo_details = json_decode($email_pattern['zoominfo_detail']);
             foreach ($zoominfo_details as $zoominfo_detail) {
                 if (isset($zoominfo_detail->DirectPhone)) {
                     $zoominfo_detail->DirectPhone = $this->filterPhoneNumber($zoominfo_detail->DirectPhone);
                     $phone_record = array('domain_id' => $domain_id, 'phone' => $zoominfo_detail->DirectPhone, 'type' => 'direct', 'is_current' => 0);
                     array_push($phone_number, $phone_record);
                     //echo 'Direct Phone :'.$zoominfo_detail->DirectPhone.'<br>';
                 }
                 if (isset($zoominfo_detail->CompanyPhone)) {
                     $zoominfo_detail->CompanyPhone = $this->filterPhoneNumber($zoominfo_detail->CompanyPhone);
                     $phone_record = array('domain_id' => $domain_id, 'phone' => $zoominfo_detail->CompanyPhone, 'type' => 'company', 'is_current' => 0);
                     array_push($phone_number, $phone_record);
                     // echo 'Company :'.$zoominfo_detail->CompanyPhone.'<br>';
                 }
             }
         }
     }
     $compnay_domain_detail = CompanyDomain::find($domain_id);
     // Domain Data
     $full_contact_company_api_call = json_decode($compnay_domain_detail->fullcontact_company);
     //Full Contact Comapny API Call
     $visistat_company_api_call = json_decode($compnay_domain_detail->visistat);
     //Visistat API CALL for Website
     if ($full_contact_company_api_call != '' && $full_contact_company_api_call->status == 200) {
         $organizaiton_detail = $full_contact_company_api_call->organization;
         if (isset($organizaiton_detail->contactInfo->phoneNumbers)) {
             $phoneNumbers = $organizaiton_detail->contactInfo->phoneNumbers;
             foreach ($phoneNumbers as $phoneNumber) {
                 $phoneNumber->number = $this->filterPhoneNumber($phoneNumber->number);
                 $phone_record = array('domain_id' => $domain_id, 'phone' => $phoneNumber->number, 'type' => 'company', 'is_current' => 0);
                 array_push($phone_number, $phone_record);
             }
         }
     }
     if ($visistat_company_api_call != '' && $visistat_company_api_call->status == 'success') {
         $organizaiton_detail = $visistat_company_api_call->data;
         if (isset($organizaiton_detail[0]->phone) && $organizaiton_detail[0]->phone != '') {
             $organizaiton_detail[0]->phone = $this->filterPhoneNumber($organizaiton_detail[0]->phone);
             $phone_record = array('domain_id' => $domain_id, 'phone' => $organizaiton_detail[0]->phone, 'type' => 'company', 'is_current' => 0);
             array_push($phone_number, $phone_record);
         }
     }
     $is_current = FALSE;
     $unique_array = array();
     $final_array = array();
     if (sizeof($phone_number) == 1) {
         $phone_number[0]['is_current'] = 1;
         $final_array = $phone_number;
     } elseif (sizeof($phone_number) > 1) {
         $i = 0;
         foreach ($phone_number as $key => $phone) {
             if (!in_array($phone['phone'], $unique_array)) {
                 array_push($unique_array, $phone['phone']);
                 $final_array[$i] = $phone;
                 $final_array[$i]['count'] = 1;
                 if ($phone['type'] == 'direct' && $is_current == FALSE) {
                     $final_array[$i]['is_current'] = 1;
                     $is_current = TRUE;
                 }
                 $i = $i + 1;
             } else {
                 foreach ($final_array as $skey => $final_arr) {
                     if ($phone['phone'] == $final_arr['phone']) {
                         $final_array[$skey]['count'] = $final_array[$skey]['count'] + 1;
                         if ($final_arr['type'] == 'direct' && $is_current == FALSE) {
                             $final_array[$skey]['is_current'] = 1;
                             $is_current = TRUE;
                         }
                         break;
                     }
                 }
             }
         }
     }
     //return $final_array;
     if ($final_array != NULL) {
         PhoneValidator::where('domain_id', '=', $domain_id)->delete();
         //Remove old phone number
         DB::table('phone_validator')->insert($final_array);
         //Insert New phone numbers
         if ($is_current == FALSE) {
             // mark current - Phone having highest count
             $highest_phone_count = PhoneValidator::where('domain_id', '=', $domain_id)->orderBy('count', 'desc')->first();
             $highest_phone_count->is_current = 1;
             $highest_phone_count->save();
         }
     }
 }