/** * @inheritDoc */ public function save(\Customer\Entity\Address $record) { $hydrator = new ClassMethods(true); $country = null; // get the country name if ($record->getCountryId()) { $country = $this->countryService->find($record->getCountryId()); $strCountry = $country->getName(); } // prepare the address string $strAddress = $record->getStreet() . " " . $record->getCode() . " " . $record->getCity() . " " . $strCountry; // get the data by Google Maps $request = new \GoogleMaps\Request(); $request->setAddress($strAddress); $proxy = new \GoogleMaps\Geocoder(); $response = $proxy->geocode($request); $results = $response->getResults(); if (isset($results[0])) { $geometry = $results[0]->getGeometry()->getLocation(); $record->setLatitude($geometry->getLat()); $record->setLongitude($geometry->getLng()); } // extract the data from the object $data = $hydrator->extract($record); $id = (int) $record->getId(); $this->getEventManager()->trigger(__FUNCTION__ . '.pre', null, array('data' => $data)); // Trigger an event if ($id == 0) { unset($data['id']); $this->tableGateway->insert($data); // add the record $id = $this->tableGateway->getLastInsertValue(); } else { $rs = $this->find($id); if (!empty($rs)) { $this->tableGateway->update($data, array('id' => $id)); } else { throw new \Exception('Record ID does not exist'); } } $record = $this->find($id); $this->getEventManager()->trigger(__FUNCTION__ . '.post', null, array('id' => $id, 'data' => $data, 'record' => $record)); // Trigger an event return $record; }