Skip to content

Commit

Permalink
Add event to add product data on indexation and filter on search
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent committed Oct 29, 2019
1 parent aae2e5b commit 2153ec6
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 133 deletions.
11 changes: 9 additions & 2 deletions Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use ElasticProduct\ElasticProduct;
use ElasticProduct\Event\SearchEvent\SearchFilterEvent;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
Expand Down Expand Up @@ -35,7 +36,8 @@ public function searchAction(Request $request)

$filters = [];

$filters[] = [
$filters[] =
[
"term" => [
"product.is_visible" => true
]
Expand Down Expand Up @@ -157,11 +159,16 @@ public function searchAction(Request $request)
];
}

$searchFilterEvent = new SearchFilterEvent($request);
$searchFilterEvent->setFilters($filters);
$this->dispatch(SearchFilterEvent::GET_SEARCH_FILTER_EVENT, $searchFilterEvent);


$body = [
"size" => 100,
"query" => [
"bool" => [
"filter" => $filters,
"filter" => $searchFilterEvent->getFilters(),
"must" => [
"bool" => [
"should" => [
Expand Down
88 changes: 88 additions & 0 deletions Event/IndexationEvent/ProductIndexationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace ElasticProduct\Event;

use Thelia\Core\Event\ActionEvent;
use Thelia\Model\Product;

class ProductIndexationEvent extends ActionEvent
{
CONST GET_PRODUCT_DATA_EVENT = 'get_product_data_event';

/**
* @var Product
*/
protected $product;

/**
* @var array
*/
protected $productData;

public function __construct(Product $product)
{
$this->product = $product;
}

/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}

/**
* @param Product $product
*
* @return ProductIndexationEvent
*/
public function setProduct($product)
{
$this->product = $product;
return $this;
}

/**
* @return array
*/
public function getProductData()
{
return $this->productData;
}

/**
* @param array $productData
*
* @return ProductIndexationEvent
*/
public function setProductData(array $productData)
{
$this->productData = $productData;
return $this;
}

/**
* @param $name
* @param $value
*
* @return $this
*/
public function addProductData($name, $value)
{
$this->productData[$name] = $value;
return $this;
}

/**
* @param $name
* @param $value
*
* @return $this
*/
public function removeProductData($name)
{
unset($this->productData[$name]);
return $this;
}
}
119 changes: 0 additions & 119 deletions Event/PsePriceEvent.php

This file was deleted.

75 changes: 75 additions & 0 deletions Event/SearchEvent/SearchFilterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace ElasticProduct\Event\SearchEvent;

use Thelia\Core\Event\ActionEvent;
use Thelia\Core\HttpFoundation\Request;

class SearchFilterEvent extends ActionEvent
{
CONST GET_SEARCH_FILTER_EVENT = 'get_search_filter_event';

/**
* @var Request
*/
protected $request;

/**
* @var array
*/
protected $filters;

public function __construct(Request $request)
{
$this->request = $request;
}

/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}

/**
* @param Request $request
*
* @return SearchFilterEvent
*/
public function setRequest($request)
{
$this->request = $request;
return $this;
}

/**
* @return array
*/
public function getFilters()
{
return $this->filters;
}

/**
* @param array $filters
*
* @return SearchFilterEvent
*/
public function setFilters($filters)
{
$this->filters = $filters;
return $this;
}

/**
* @param $value
*
* @return $this
*/
public function addFilter($value)
{
$this->filters[] = $value;
return $this;
}
}
32 changes: 20 additions & 12 deletions Service/IndexationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ElasticProduct\Service;

use ElasticProduct\ElasticProduct;
use ElasticProduct\Event\ProductIndexationEvent;
use Propel\Runtime\Propel;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Thelia\Action\Image;
Expand Down Expand Up @@ -105,7 +106,7 @@ public function indexProduct(Product $product)
'product' => $this->getProductData($product),
'brand' => $this->getBrandData($product),
'categories' => $this->getCategoriesData($product),
'features' => $this->getFeaturesData($product),
'features' => $this->getFeaturesData($product)
];

$productSaleElementsRelatedBody = [];
Expand Down Expand Up @@ -229,7 +230,7 @@ protected function getProductSaleElementsData(ProductSaleElements $productSaleEl
];
}

protected function getFeaturesData(Product $product)
protected function getFeaturesData(Product $product)
{
$productFeatures = $product->getFeatureProducts();
$features = [];
Expand Down Expand Up @@ -300,16 +301,23 @@ protected function getProductData(Product $product)
$langData = $this->getLangData($product->getProductI18ns(), true, $product, 'product');
$imageData = $this->getImageData($product->getProductImages(), 'product');

return [
'id' => $product->getId(),
'ref' => $product->getRef(),
'is_visible' => !!$product->getVisible(),
'i18ns' => $langData['i18ns'],
'urls' => $langData['urls'],
'images' => $imageData,
'created_at' => $product->getCreatedAt('Y-m-d H:i:s'),
'updated_at' => $product->getUpdatedAt('Y-m-d H:i:s')
];
$productIndexationEvent = new ProductIndexationEvent($product);
$productIndexationEvent->setProductData(
[
'id' => $product->getId(),
'ref' => $product->getRef(),
'is_visible' => !!$product->getVisible(),
'i18ns' => $langData['i18ns'],
'urls' => $langData['urls'],
'images' => $imageData,
'created_at' => $product->getCreatedAt('Y-m-d H:i:s'),
'updated_at' => $product->getUpdatedAt('Y-m-d H:i:s')
]
);
$this->eventDispatcher->dispatch(ProductIndexationEvent::GET_PRODUCT_DATA_EVENT, $productIndexationEvent);


return $productIndexationEvent->getProductData();
}

protected function getLangData($i18ns, $withUrl = false, $model = null, $viewName = '')
Expand Down

0 comments on commit 2153ec6

Please sign in to comment.