/**
  * @param Address $address
  *
  * @return string
  * @throws \Exception
  */
 public function generateFormattedAddress(Address $address)
 {
     $formattedAddress = '';
     $streetName = $address->getStreetName();
     $streetNumber = $address->getStreetNumber();
     if ($streetName && $streetNumber) {
         $formattedAddress = sprintf('%s %s', $streetNumber, $streetName);
     } else {
         if ($streetName) {
             $formattedAddress = $streetName;
         }
     }
     return $formattedAddress;
 }
 /**
  * @ApiDoc(
  *  description="Check Public Api Status",
  *  statusCodes={200="Success"},
  *  section="Public Api"
  * )
  * @Route("/public-api/performance")
  * @Template{}
  * @Method({"GET"})
  * @return mixed
  */
 public function performanceAction()
 {
     $stopwatch = new Stopwatch();
     $stopwatch->start('main');
     $address = new Address();
     $address->setCity('London');
     $address->setCountry('GB');
     $address->setLat('51.5286416');
     $address->setLng('-0.1015987');
     $geoname = $this->geonameRepository->getOneByAddress($address);
     $stopwatch->lap('main');
     $cuisines = $this->cuisineRepository->findAll();
     $event = $stopwatch->stop('main');
     $duration = $event->getDuration();
     $endTime = $event->getEndTime();
     $memory = $event->getMemory();
     $periodsData = array();
     foreach ($event->getPeriods() as $period) {
         $periodsData[] = array('duration' => $period->getDuration(), 'memory' => $period->getMemory());
     }
     return $this->view(array('duration' => $duration, 'endTime' => $endTime, 'memory' => $memory, 'periods' => $periodsData, 'geoname' => $geoname, 'cuisines' => $cuisines));
 }
 /**
  * @param $venue
  *
  * @return Geoname
  */
 public function getGeoname($venue)
 {
     $address = new Address();
     $address->setLat($venue['location']['lat']);
     $address->setLng($venue['location']['lng']);
     if (isset($venue['location']['city'])) {
         $address->setCity($venue['location']['city']);
     }
     if (isset($venue['location']['country'])) {
         $address->setCountry($venue['location']['country']);
     }
     if (isset($venue['location']['state'])) {
         $address->setCounty($venue['location']['state']);
     }
     if (isset($venue['location']['postalCode'])) {
         $address->setPostalCode($venue['location']['postalCode']);
     }
     $geoname = $this->geonameRepository->getOneByAddress($address);
     return $geoname;
 }
 /**
  * @param int $id
  *
  * @ApiDoc(
  *  description="GooglePlace search",
  *  statusCodes={200="Address GooglePlace"},
  *  section="Google Place")
  * @Route("/api/google-places/place-results/{id}")
  * @throws BadRequestException
  * @Method({"GET"})
  * @Cache(maxage="+1 week", public=true)
  * @return View
  */
 public function getAction($id)
 {
     $place = $this->googlePlaceClient->detail($id);
     $address = new Address();
     $address->setCity($place->getCity());
     $address->setLat($place->getLatitude());
     $address->setLng($place->getLongitude());
     $geoname = $this->geonameRepository->getOneByAddress($address);
     $place->setGeoname($geoname);
     return $this->view(array('placeResult' => $place));
 }
 /**
  * @param FullPlaceResult $place
  * @return Address
  *
  * @throws UnresolvedGeonameException
  */
 public function createFromFullPlaceResult(FullPlaceResult $place)
 {
     $address = new Address();
     $address->setCity($place->getCity());
     $address->setLat($place->getLatitude());
     $address->setLng($place->getLongitude());
     $address->setCountry($place->getCountry());
     $address->setCountryCode($place->getCountryCode());
     $address->setFormattedAddress($place->getFormattedAddress());
     $address->setRegion($place->getRegion());
     $address->setRegionCode($place->getRegionCode());
     $address->setCounty($place->getCounty());
     $address->setCountyCode($place->getCountyCode());
     $address->setCity($place->getCity());
     $address->setPostalCode($place->getPostalCode());
     $address->setStreetName($place->getStreetName());
     $address->setStreetNumber($place->getStreetNumber());
     $geoname = $this->geonameRepository->getOneByAddress($address);
     $address->setGeoname($geoname);
     return $address;
 }
 /**
  * @param Address $address
  * @param string  $precision
  *
  * @return bool
  */
 public function isAccurate(Address $address, $precision)
 {
     if ($precision === GeocodePrecision::COUNTRY && is_null($address->getCountry())) {
         return false;
     } else {
         if ($precision === GeocodePrecision::CITY && is_null($address->getCity())) {
             return false;
         } else {
             if ($precision === GeocodePrecision::STREET_NAME && is_null($address->getStreetName())) {
                 return false;
             } else {
                 if ($precision === GeocodePrecision::STREET_NUMBER && is_null($address->getStreetNumber())) {
                     return false;
                 }
             }
         }
     }
     return true;
 }
 /**
  * @param Address $address
  *
  * @return mixed
  * @throws UnresolvedGeonameException
  */
 public function getNearestCity(Address $address)
 {
     $queryBuilder = $this->getEntityManager()->createQueryBuilder()->select('g')->addSelect('DISTANCE(g.lat,g.lng,:lat,:lng) AS HIDDEN distance')->from(Geoname::SHORTCUT_CLASS_NAME, 'g')->leftJoin('g.translations', 't')->setParameter('lat', $address->getLat())->setParameter('lng', $address->getLng())->orderBy('distance', 'ASC');
     $query = $queryBuilder->getQuery()->setMaxResults(1);
     $results = $query->getResult();
     if (is_array($results) && count($results) > 0) {
         return $results[0];
     }
     throw new UnresolvedGeonameException();
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->formatOutput($output);
     $this->getDependencies();
     $latitude = $input->getOption('latitude');
     $longitude = $input->getOption('longitude');
     $radius = $input->getOption('radius');
     $city = $input->getOption('city');
     if (!$city && !($latitude && $longitude)) {
         return $output->writeln('<error>Insert city parameter or latitude + longitude.</error>');
     }
     if ($city) {
         $geocodedAddress = $this->geocoder->geocode($city);
         $latitude = $geocodedAddress->getLat();
         $longitude = $geocodedAddress->getLng();
     }
     $restaurants = array();
     $nextPage = null;
     do {
         $output->writeln('Searching in google place');
         $placeResults = $this->googlePlaceClient->search($latitude, $longitude, null, $radius, $nextPage);
         $nextPage = $placeResults->getNextPage();
         foreach ($placeResults->toArray() as $placeResult) {
             $restaurant = new Restaurant();
             $fullPlaceResult = $this->googlePlaceClient->detail($placeResult->getId());
             $restaurant->setName($placeResult->getName());
             $restaurant->setLat($fullPlaceResult->getLatitude());
             $restaurant->setLng($fullPlaceResult->getLongitude());
             $restaurant->setAddress($fullPlaceResult->getFormattedAddress());
             $address = new Address();
             $address->setCity($fullPlaceResult->getCity());
             $address->setLat($fullPlaceResult->getLatitude());
             $address->setLng($fullPlaceResult->getLongitude());
             $address->setFormattedAddress($fullPlaceResult->getFormattedAddress());
             $this->addressManager->hydrateGeoname($address);
             $restaurant->setGeoname($address->getGeoname());
             $photosReferences = $fullPlaceResult->getPhotoReferences();
             if (count($photosReferences) > 0) {
                 $message = $this->googlePlaceClient->image($photosReferences[0]);
                 $imageContent = $message->getContent();
                 $filename = $this->s3->uploadData($imageContent, 'restaurant/');
                 $restaurant->setPicture($filename);
             }
             $errors = $this->validator->validate($restaurant);
             if (count($errors) > 0) {
                 $output->writeln(sprintf('<error>Import error:</error> %s ', (string) $errors));
             } else {
                 $restaurants[] = $restaurant;
                 $this->entityManager->persist($restaurant);
                 $output->writeln(sprintf('Imported: <info>%s</info> - %s', $restaurant->getName(), $address->getFormattedAddress()));
             }
         }
     } while ($nextPage);
     $this->entityManager->flush();
     $output->writeln(sprintf('<success>Imported successfully %d Restaurants</success>', count($restaurants)));
 }