Skip to content

Commit

Permalink
Luigi's Box code rewritten from Persoo to Luigi's Box
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Feb 22, 2024
1 parent 38fa4af commit cdd98f9
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 256 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ composer require shopsys/luigis-box
Set these environment variables in your project:

```
LUIGIS_BOX_IS_PRODUCTION_MODE // 'true' or 'false'
LUIGIS_BOX_ENABLED_DOMAIN_IDS // comma separated list of domain ids e.g. '1,2'
LUIGIS_BOX_ACCOUNT_ID // value provided by LuigisBox company
LUIGIS_BOX_RECOMMENDATION_ALGORITHM_ID // value provided by LuigisBox company
LUIGIS_BOX_RECOMMENDATION_OFFER_ID // value provided by LuigisBox company
LUIGIS_BOX_RECOMMENDATION_LOCATION_ID // value provided by LuigisBox company
LUIGIS_BOX_SEARCH_ALGORITHM_ID // value provided by LuigisBox company
LUIGIS_BOX_SEARCH_OFFER_ID // value provided by LuigisBox company
LUIGIS_BOX_SEARCH_LOCATION_ID // value provided by LuigisBox company
LUIGIS_BOX_TRACKER_IDS_BY_DOMAIN_IDS='{"1": "<TRACKER_ID_FOR_DOMAIN_1>", "2": "<TRACKER_ID_FOR_DOMAIN_2>", ...}' // JSON object with domain id as key and tracker id as value
```

## Contributing
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "shopsys/luigisBox",
"name": "shopsys/luigis-box",
"type": "library",
"description": "Shopsys Platform connector to Luigi's Box",
"keywords": ["api", "Shopsys Platform", "Luigi's Box", "SSFW", "SSP"],
"keywords": ["api", "Shopsys Platform", "Luigis Box", "SSFW", "SSP"],
"license": "proprietary",
"authors": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Shopsys\LuigisBoxBundle\Component\LuigisBox\Filter;

use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\String\TransformString;
use Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade;
use Shopsys\FrameworkBundle\Model\Product\Brand\Brand;
use Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData;

class ProductFilterToLuigisBoxFilterMapper
{
public const string FILTER_OR = 'f';
public const string FILTER_AND = 'f_must';

/**
* @param \Shopsys\FrameworkBundle\Model\Product\Availability\ProductAvailabilityFacade $productAvailabilityFacade
*/
Expand All @@ -21,51 +22,51 @@ public function __construct(
}

/**
* @param string $luigisBoxType
* @param \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData $productFilterData
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @return array
*/
public function mapForSearch(ProductFilterData $productFilterData, Domain $domain): array
public function mapForSearch(string $luigisBoxType, ProductFilterData $productFilterData, Domain $domain): array
{
$luigisBoxFilter = [];
$luigisBoxFilter = [
self::FILTER_AND => [],
self::FILTER_OR => [],
];

$luigisBoxFilter = $this->mapType($luigisBoxType, $luigisBoxFilter);
$luigisBoxFilter = $this->mapPrice($productFilterData, $luigisBoxFilter);
$luigisBoxFilter = $this->mapAvailability($productFilterData, $luigisBoxFilter, $domain->getLocale());
$luigisBoxFilter = $this->mapAvailability($productFilterData, $luigisBoxFilter);
$luigisBoxFilter = $this->mapFlags($productFilterData, $luigisBoxFilter, $domain->getLocale());
$luigisBoxFilter = $this->mapBrands($productFilterData, $luigisBoxFilter);

return $luigisBoxFilter;
}

/**
* @param string $luigisBoxType
* @param array $luigisBoxFilter
* @return array
*/
protected function mapType(string $luigisBoxType, array $luigisBoxFilter): array
{
$luigisBoxFilter[self::FILTER_OR][] = 'type:' . $luigisBoxType;

return $luigisBoxFilter;
}

/**
* @param \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData $productFilterData
* @param array $luigisBoxFilter
* @return array
*/
protected function mapPrice(ProductFilterData $productFilterData, array $luigisBoxFilter): array
{
if ($productFilterData->minimalPrice !== null) {
$luigisBoxFilter['must'][] = [
'type' => 'customRule',
'fields' => [
'price',
'$gte',
'value',
$productFilterData->minimalPrice->getAmount(),
],
];
}
if ($productFilterData->minimalPrice !== null || $productFilterData->maximalPrice !== null) {
$priceFrom = $productFilterData->minimalPrice === null ? '' : $productFilterData->minimalPrice->getAmount();
$priceTo = $productFilterData->maximalPrice === null ? '' : $productFilterData->maximalPrice->getAmount();

if ($productFilterData->maximalPrice !== null) {
$luigisBoxFilter['must'][] = [
'type' => 'customRule',
'fields' => [
'price',
'$lte',
'value',
$productFilterData->maximalPrice->getAmount(),
],
];
$luigisBoxFilter[self::FILTER_AND][] = 'price:' . $priceFrom . '|' . $priceTo;
}

return $luigisBoxFilter;
Expand All @@ -74,26 +75,14 @@ protected function mapPrice(ProductFilterData $productFilterData, array $luigisB
/**
* @param \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData $productFilterData
* @param array $luigisBoxFilter
* @param string $locale
* @return array
*/
protected function mapAvailability(
ProductFilterData $productFilterData,
array $luigisBoxFilter,
string $locale,
): array {
if ($productFilterData->inStock === true) {
$luigisBoxFilter['must'] = [
[
'type' => 'customRule',
'fields' => [
'availability',
'$in',
'value',
$this->productAvailabilityFacade->getOnStockText($locale),
],
],
];
$luigisBoxFilter[self::FILTER_AND][] = 'availability:1';
}

return $luigisBoxFilter;
Expand All @@ -109,15 +98,7 @@ protected function mapFlags(ProductFilterData $productFilterData, array $luigisB
{
if (count($productFilterData->flags) > 0) {
foreach ($productFilterData->flags as $flag) {
$luigisBoxFilter['must'][] = [
'type' => 'customRule',
'fields' => [
'tag' . TransformString::safeFilename($flag->getName($locale)),
'$in',
'value',
'true',
],
];
$luigisBoxFilter[self::FILTER_OR][] = 'labels:' . $flag->getName($locale);
}
}

Expand All @@ -132,17 +113,9 @@ protected function mapFlags(ProductFilterData $productFilterData, array $luigisB
protected function mapBrands(ProductFilterData $productFilterData, array $luigisBoxFilter): array
{
if (count($productFilterData->brands) > 0) {
$luigisBoxFilter['must'] = [
[
'type' => 'customRule',
'fields' => [
'brand',
'$in',
'value',
'"' . implode('","', array_map(static fn (Brand $brand) => $brand->getName(), $productFilterData->brands)) . '"',
],
],
];
foreach ($productFilterData->brands as $brand) {
$luigisBoxFilter[self::FILTER_OR][] = 'brand:' . $brand->getName();
}
}

return $luigisBoxFilter;
Expand Down
Loading

0 comments on commit cdd98f9

Please sign in to comment.