/** * @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))); }
/** * @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)); }