/**
  * Try to guess the cuisine by restaurant name
  * otherwise return random cuisine
  *
  * @param Restaurant $restaurant
  * @param array      $cuisines
  *
  * @return Cuisine
  */
 public function guessCuisine(Restaurant $restaurant, array $cuisines)
 {
     $name = strtolower($restaurant->getName());
     /** @var Cuisine $cuisine */
     foreach ($cuisines as $cuisine) {
         if (strpos($name, strtolower($cuisine->getName())) !== false) {
             return $cuisine;
         }
     }
     return $cuisines[array_rand($cuisines)];
 }
 /**
  * @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');
     }
 }
 /**
  * @param Restaurant $restaurant
  *
  * @return Restaurant|null
  */
 public function getRestaurantByRestaurant(Restaurant $restaurant)
 {
     $results = $this->getEntityManager()->createQueryBuilder()->select('r')->from(Restaurant::SHORTCUT_CLASS_NAME, 'r')->where('r.name = :name')->andWhere('r.lat = :lat')->andWhere('r.lng = :lng')->setParameter('name', $restaurant->getName())->setParameter('lat', $restaurant->getLat())->setParameter('lng', $restaurant->getLng())->getQuery()->getResult();
     if (count($results) > 0) {
         return $results[0];
     }
     return null;
 }
 /**
  * @param Restaurant $restaurant
  *
  * @return integer
  */
 public function getAverageCost(Restaurant $restaurant)
 {
     $sql = 'SELECT AVG(cost) as averageCost FROM reviews WHERE restaurant_id = :restaurant_id';
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('restaurant_id', $restaurant->getId());
     $stmt->execute();
     $results = $stmt->fetch();
     return (int) $results["averageCost"];
 }
 /**
  * @param Restaurant $restaurant
  * @param $venue
  */
 protected function setCuisine(Restaurant $restaurant, $venue)
 {
     $catName = $this->getPrimaryCuisineName($venue);
     $cuisine = $this->cuisineRepository->getCuisineByName($catName);
     if (!$cuisine) {
         $cuisine = $this->cuisineRepository->getDefaultCuisine();
     }
     $restaurant->setCuisine($cuisine);
 }
 /**
  * @return array
  */
 public function getMeta()
 {
     return array('coordinate' => array($this->restaurant->getLng(), $this->restaurant->getLat()));
 }
 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)));
 }