/**
  * @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)));
 }
 /**
  * @ApiDoc(
  *  description="New Photo",
  *  statusCodes={201="Photo"},
  *  section="Photo",
  *  parameters={
  *      {"name"="uploadedPicture", "dataType"="string", "required"=false },
  *      {"name"="restaurant[id]", "dataType"="integer", "required"=true },
  *  })
  * @Route("/api/photos")
  * @Method({"POST"})
  * @return View
  */
 public function newAction()
 {
     $user = $this->getUser();
     /** @var Photo $photo */
     $photo = $this->deserializeCreateRequest(Photo::CLASS_NAME);
     $photo->setUser($user);
     if ($photo->getUploadedPicture()) {
         $pictureFilename = $this->s3Client->uploadBase64($photo->getUploadedPicture(), 'photos/', 'jpg', 1080, 1080);
         $photo->setSrc($pictureFilename);
         $thumbFilename = $this->s3Client->uploadBase64($photo->getUploadedPicture(), 'photos_thumb/', 'jpg', 200, 200);
         $photo->setThumb($thumbFilename);
     }
     $this->validate($photo);
     $this->entityManager->persist($photo);
     $this->entityManager->flush();
     $this->dispatch(ApiEvent::PHOTO_CREATED, new PhotoCreatedEvent($photo, $this->getUser()));
     $this->cacheManager->invalidateOnInsert($photo);
     return $this->view(array('photo' => $photo), 201);
 }
 /**
  * @ApiDoc(
  *  description="Update Restaurant",
  *  statusCodes={200="Restaurant"},
  *  section="Restaurant")
  * @Route("/api/restaurants/{id}")
  * @Method({"PUT"})
  * @return View
  */
 public function updateAction()
 {
     $restaurant = $this->deserializeUpdateRequest(Restaurant::CLASS_NAME);
     $this->securityCheck(Permission::WRITE, $restaurant);
     // @TODO move this to restaurant manager
     if ($restaurant->getUploadedPicture()) {
         $pictureFilename = $this->s3Client->uploadBase64($restaurant->getUploadedPicture(), 'restaurant/', 'jpg', 640, 380);
         $restaurant->setPicture($pictureFilename);
         $thumbFilename = $this->s3Client->uploadBase64($restaurant->getUploadedPicture(), 'restaurant_thumb/', 'jpg', 200, 200);
         $restaurant->setThumb($thumbFilename);
     }
     $this->validate($restaurant);
     $this->entityManager->flush();
     $this->cacheManager->invalidateOnUpdate($restaurant);
     return $this->view(array('restaurant' => $restaurant));
 }