Exemplo n.º 1
0
 /**
  * @param int $from
  * @param int $size
  * @return SearchResultSlice
  */
 public function getDrinks($from = 0, $size = 20)
 {
     $criteria = new SearchEngine\Criteria($from);
     $criteria->changeSize($size);
     $criteria->updateRequiredTaste((new TasteBuilder())->sweet()->buildTaste());
     return $this->searchEngine->search($criteria);
 }
Exemplo n.º 2
0
 public function similarRecipesAction($slug, $size)
 {
     $recipe = $this->get('my_drinks.recipes')->findBySlug($slug);
     if (is_null($recipe)) {
         throw new NotFoundHttpException();
     }
     $criteria = new Criteria();
     $criteria->similarTo($recipe->getName());
     $criteria->changeSize((int) $size);
     $results = $this->get('my_drinks.search_engine')->search($criteria);
     return $this->render(':recipe:similarRecipes.html.twig', ['recipe' => $recipe, 'results' => $results]);
 }
Exemplo n.º 3
0
 public function indexAction(Request $request)
 {
     $form = $this->get('form.factory')->createNamed('', new SearchType());
     $form->handleRequest($request);
     $criteria = new Criteria((int) $request->query->get(self::PARAM_START, 0));
     $criteria->changeSize(20);
     if ($form->isValid()) {
         $criteria->decorate(new SearchCriteriaDecorator($form));
     } else {
         if ($form->isSubmitted()) {
             $ingredients = $form->get('ingredients')->getData();
             $criteria->mustContainIngredients(is_null($ingredients) ? [] : $ingredients);
         }
     }
     $drinks = $this->get('my_drinks.search_engine')->search($criteria);
     return $this->render('search/index.html.twig', ['results' => null, 'drinks' => $drinks]);
 }
Exemplo n.º 4
0
 public function createAction(Request $request)
 {
     $form = $this->createForm(new CreateRecipeType(), new CreateNewRecipeCommand());
     $searchForm = $this->createForm(new AdminSearchType());
     $form->handleRequest($request);
     $searchForm->handleRequest($request);
     if ($form->isValid()) {
         $this->get('my_drinks.command_bus')->handle($form->getData());
         $slug = $this->get('my_drinks.slug_generator')->generateFrom($form->get('name')->getData());
         return $this->redirect($this->generateUrl('admin_recipe_update_description', ['slug' => $slug]));
     }
     $criteria = new Criteria((int) $request->query->get('start', 0), false);
     $criteria->sortBy('name.raw', Criteria::SORT_ASC);
     if ($searchForm->isValid()) {
         $criteria->addQuery($searchForm->get('name')->getData());
     }
     $results = $this->get('my_drinks.search_engine')->search($criteria);
     return $this->render('admin/recipe/create.html.twig', ['search' => $searchForm->createView(), 'form' => $form->createView(), 'results' => $results]);
 }
Exemplo n.º 5
0
 /**
  * @param Criteria $criteria
  */
 public function decorate(Criteria $criteria)
 {
     $alcoholFrom = $this->form->get('alcohol')->get('from')->getData();
     $alcoholTo = $this->form->get('alcohol')->get('to')->getData();
     if (!is_null($alcoholFrom)) {
         $criteria->withAlcoholContentGreaterThan($alcoholFrom);
     }
     if (!is_null($alcoholTo)) {
         $criteria->withAlcoholContentLowerThan($alcoholTo);
     }
     $criteria->addQuery($this->form->get('query')->getData());
     $criteria->mustContainIngredients($this->form->get('ingredients')->getData());
     $tasteBuilder = new TasteBuilder();
     foreach ($this->form->get('taste')->getData() as $tasteName) {
         switch ($tasteName) {
             case Tastes::SWEET:
                 $tasteBuilder->sweet();
                 break;
             case Tastes::BITTER:
                 $tasteBuilder->bitter();
                 break;
             case Tastes::SALTY:
                 $tasteBuilder->salty();
                 break;
             case Tastes::SPICY:
                 $tasteBuilder->spicy();
                 break;
             case Tastes::SOUR:
                 $tasteBuilder->sour();
                 break;
         }
     }
     $criteria->updateRequiredTaste($tasteBuilder->buildTaste());
 }
Exemplo n.º 6
0
 /**
  * @param Criteria $criteria
  */
 private function addTasteQueryFilter(Criteria $criteria)
 {
     $requiredTaste = $criteria->getRequiredTaste();
     $optionalTaste = $criteria->getOptionalTaste();
     if ($requiredTaste->isDefined()) {
         $this->parameters['body']['query']['filtered']['filter']['and'][] = ['terms' => ['description.taste' => $this->buildTasteArray($requiredTaste)]];
     }
     if ($optionalTaste->isDefined()) {
         foreach ($this->buildTasteArray($optionalTaste) as $taste) {
             $this->parameters['body']['query']['filtered']['filter']['or'][] = ['terms' => ['description.taste' => [$taste]]];
         }
     }
 }
Exemplo n.º 7
0
 function it_build_parameters_with_alcohol_content_range()
 {
     $criteria = new Criteria(0, false);
     $criteria->withAlcoholContentLowerThan(100);
     $criteria->withAlcoholContentGreaterThan(0);
     $this->createParameters($criteria)->shouldReturn(['index' => ElasticSearch::INDEX, 'type' => 'recipe', 'body' => ['from' => 0, 'size' => 10, 'query' => ['filtered' => ['filter' => ['and' => [['range' => ['description.alcoholContent' => ['gte' => 0, 'lte' => 100]]]]]]]]]);
 }
Exemplo n.º 8
0
 public function getCurrentPage()
 {
     return (int) ($this->criteria->startFrom() / $this->criteria->getSize());
 }