Skip to content

Commit

Permalink
Adjust AttributeRule checker to new Bitbag\Elasticsearch-Plugin (3.2.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherhero committed Jul 13, 2023
1 parent dfe8fce commit 71b43cc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Checker/Rule/Elasticsearch/AttributeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
namespace BitBag\SyliusCatalogPlugin\Checker\Rule\Elasticsearch;

use BitBag\SyliusElasticsearchPlugin\Formatter\StringFormatterInterface;
use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\AttributeBuilder;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Query\AbstractQuery;
use Elastica\Query\BoolQuery;
use Elastica\Query\Term;
use Sylius\Component\Attribute\AttributeType\DateAttributeType;
use Sylius\Component\Attribute\AttributeType\DatetimeAttributeType;
use Sylius\Component\Attribute\Model\AttributeInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;

Expand All @@ -40,17 +44,40 @@ public function createSubquery(array $configuration): AbstractQuery
/** @var AttributeInterface $attribute */
$attribute = $configuration['attribute'];

if (!$attribute instanceof AttributeInterface) {
return new BoolQuery();
}

$paramName = \sprintf(
'%s_%s.keyword',
$this->attributeNameResolver->resolvePropertyName($attribute->getCode()),
$this->localeContext->getLocaleCode()
);
$value = $this->stringFormatter->formatToLowercaseWithoutSpaces($configuration['value']);

if (in_array($attribute->getStorageType(), [DateAttributeType::TYPE, DatetimeAttributeType::TYPE])) {
$value = $this->getDateAttributeValue($configuration);
} else {
$value = $this->stringFormatter->formatToLowercaseWithoutSpaces($configuration['value']);
}

$term = new Term();
$term->setTerm($paramName, $value);

/* @phpstan-ignore-next-line Elastica\Query\Term Class extended by Elastica\Query\AbstractQuery*/
return $term;
}

private function getDateAttributeValue(array $configuration): string
{
$storageType = $configuration['attribute']->getStorageType();
$format = $configuration['attribute']->getConfiguration()['format'] ??
(
DateAttributeType::TYPE === $storageType ?
AttributeBuilder::DEFAULT_DATE_FORMAT :
AttributeBuilder::DEFAULT_DATE_TIME_FORMAT
)
;

return (new \DateTime($configuration['value'] ?? 'now'))->format($format);
}
}

0 comments on commit 71b43cc

Please sign in to comment.