/**
  * @param Review $review
  */
 private function updateRestaurantAverageData(Review $review)
 {
     $restaurant = $review->getRestaurant();
     $averageCost = $this->reviewRepository->getAverageCost($review->getRestaurant());
     $cuisine = $this->cuisineRepository->getMostUsedCuisine($review->getRestaurant());
     $restaurant->setAverageCost($averageCost);
     $restaurant->setCuisine($cuisine);
 }
 /**
  * @param Review $review
  */
 protected function cleanComment(Review $review)
 {
     $comment = $review->getComment();
     $pattern = '/(#\\w+)/';
     $replacement = '';
     $comment = preg_replace($pattern, $replacement, $comment);
     $comment = preg_replace('!\\s+!', ' ', $comment);
     $review->setComment($comment);
 }
 /**
  * @return array
  */
 public function getMeta()
 {
     return array('coordinate' => array($this->review->getRestaurant()->getLng(), $this->review->getRestaurant()->getLat()));
 }
 /**
  * @return User
  */
 public function getUser()
 {
     return $this->review->getUser();
 }
 /**
  * @param Review $review
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 public function decrementReviewsWithHigherPosition(Review $review)
 {
     $cuisine = $review->getCuisine();
     $geoname = $review->getGeoname();
     $user = $review->getUser();
     $sql = 'UPDATE reviews
             SET position = position - 1
             WHERE user_id = ?
               AND geoname_id = ?
               AND cuisine_id = ?
               AND position > ?';
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue(1, $user->getId());
     $stmt->bindValue(2, $geoname->getId());
     $stmt->bindValue(3, $cuisine->getId());
     $stmt->bindValue(4, $review->getPosition());
     $stmt->execute();
 }
 /**
  * Remove reviews
  *
  * @param Review $review
  */
 public function removeReview(Review $review)
 {
     $review->setRestaurant(null);
     $this->reviews->removeElement($review);
 }
 /**
  * @ApiDoc(
  *  description="New Batch of Reviews",
  *  statusCodes={201="Batch of Reviews"},
  *  section="Review",
  *  parameters={
  *      {"name"="restaurants", "dataType"="integer", "required"=true }
  * })
  * @Route("/api/reviews/batch")
  * @Method({"POST"})
  * @return View
  */
 public function batchInsertAction()
 {
     $user = $this->getUser();
     $positions = array();
     $request = $this->requestStack->getCurrentRequest();
     $ids = $request->request->get('restaurants');
     $restaurants = $this->restaurantRepository->getByIds($ids);
     $reviews = array();
     /** @var Restaurant $restaurant */
     foreach ($restaurants as $restaurant) {
         if (null === $restaurant->getGeoname() || null === $restaurant->getCuisine()) {
             continue;
         }
         if (isset($positions[$restaurant->getRankingKey()])) {
             $positions[$restaurant->getRankingKey()]++;
         } else {
             $positions[$restaurant->getRankingKey()] = 1;
         }
         $review = new Review();
         $review->setGeoname($restaurant->getGeoname());
         $review->setCost($restaurant->getAverageCost());
         $review->setCuisine($restaurant->getCuisine());
         $review->setPosition($positions[$restaurant->getRankingKey()]);
         $review->setUser($user);
         $review->setComment('');
         $review->setRestaurant($restaurant);
         $reviews[] = $review;
         $this->validate($review);
         $user->incrementReviewsCount();
         $this->entityManager->persist($review);
         $this->cacheManager->invalidateOnInsert($review);
     }
     $this->entityManager->flush();
     return $this->view(array('reviews' => $reviews), 201);
 }
 /**
  * @param Review $review
  */
 public function autoReorderRankingOfReview(Review $review)
 {
     $this->autoReorderRanking($review->getUser()->getId(), $review->getGeoname()->getId(), $review->getCuisine()->getId());
 }