/** * @param Restaurant $restaurant */ protected function hydratePictureAndThumb(Restaurant $restaurant) { if ($restaurant->getPicture()) { $restaurant->setPicture($this->cloudfrontUrl . $restaurant->getPicture()); } else { $restaurant->setPicture($this->cloudfrontUrl . 'restaurant/_default_restaurant_cover.png'); } if ($restaurant->getThumb()) { $restaurant->setThumb($this->cloudfrontUrl . $restaurant->getThumb()); } else { $restaurant->setThumb($this->cloudfrontUrl . 'restaurant_thumb/_default_restaurant.png'); } }
public function create() { $geoname = $this->getRandomGeoname(); $restaurant = new Restaurant(); $restaurant->setWebsite('http://www.google.com'); $restaurant->setAddress($this->getRandomAddress()); $restaurant->setLat($this->getRandomLat($geoname)); $restaurant->setLng($this->getRandomLng($geoname)); $restaurant->setAverageCost(rand(0, 200)); $restaurant->setName($this->getRandomName()); $restaurant->setCuisine($this->getRandomCuisine()); $restaurant->setGeoname($geoname); $restaurant->setPhone('04422556677'); $restaurant->setPicture(null); $restaurant->setThumb(null); return $restaurant; }
/** * @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))); }