protected function getLeadAttributeValue(Lead $lead, $key, $default = "", $desc = false, $exact = false)
 {
     $attribute = $lead->findAttribute($key, $desc, $exact);
     return $attribute ? $attribute->getValue() : $default;
 }
 public function updateLeadGeo(Lead $lead, $location = [])
 {
     $result = false;
     $update = false;
     if ($location && isset($location['_source'])) {
         $locality = $location['_source'];
         $em = $this->getEntityManager();
         $attributeRepo = $em->getRepository('Lead\\Entity\\LeadAttribute');
         // Find missing fields, if any
         $existing = [];
         $existing['locality'] = $lead->getLocality();
         foreach (['city', 'state', 'zip'] as $attribute) {
             $value = $lead->findAttribute($attribute, true);
             $existing[$attribute] = $value ? $value->getValue() : null;
         }
         foreach ($existing as $field => $value) {
             if (empty($value)) {
                 switch ($field) {
                     case 'locality':
                         $lead->setLocality(implode(",", $locality['latlon']));
                         $update = true;
                         break;
                     default:
                         $updated = false;
                         switch ($field) {
                             case 'state':
                                 $updated = $locality[$field]['abbrev'];
                                 break;
                             default:
                                 $updated = $locality[$field];
                                 break;
                         }
                         if ($updated) {
                             $attribute_id = $attributeRepo->getIDFromDesc(ucwords($field), $field == 'zip');
                             if ($attribute_id) {
                                 $attribute = $attributeRepo->find($attribute_id);
                                 if ($attribute) {
                                     $leadAttributeValue = new LeadAttributeValue();
                                     $leadAttributeValue->setValue($updated);
                                     $leadAttributeValue->setAttribute($attribute);
                                     $lead->addAttribute($leadAttributeValue);
                                     $update = true;
                                 }
                             }
                         }
                         break;
                 }
             }
         }
         if ($update) {
             try {
                 $em->merge($lead);
                 $em->flush();
                 $result = true;
             } catch (\Exception $e) {
                 $result = false;
             }
         }
     }
     return $result;
 }