BitBagCommerce /
SyliusElasticsearchPlugin
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file was created by developers working at BitBag |
||
| 5 | * Do you need more information about us and what we do? Visit our https://bitbag.io website! |
||
| 6 | * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
||
| 7 | */ |
||
| 8 | |||
| 9 | declare(strict_types=1); |
||
| 10 | |||
| 11 | namespace BitBag\SyliusElasticsearchPlugin\Facet; |
||
| 12 | |||
| 13 | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
||
| 14 | use Elastica\Aggregation\AbstractAggregation; |
||
| 15 | use Elastica\Aggregation\Terms; |
||
| 16 | use Elastica\Query\AbstractQuery; |
||
| 17 | use Elastica\Query\Terms as TermsQuery; |
||
| 18 | use Sylius\Component\Attribute\Model\AttributeInterface; |
||
| 19 | use Sylius\Component\Locale\Context\LocaleContextInterface; |
||
| 20 | use Sylius\Component\Resource\Repository\RepositoryInterface; |
||
| 21 | |||
| 22 | final class AttributeFacet implements FacetInterface |
||
| 23 | { |
||
| 24 | /** @var ConcatedNameResolverInterface */ |
||
| 25 | private $attributeNameResolver; |
||
| 26 | |||
| 27 | /** @var RepositoryInterface */ |
||
| 28 | private $productAttributeRepository; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | private $attributeCode; |
||
| 32 | |||
| 33 | /** @var LocaleContextInterface */ |
||
| 34 | private $localeContext; |
||
| 35 | |||
| 36 | public function __construct( |
||
| 37 | ConcatedNameResolverInterface $attributeNameResolver, |
||
| 38 | RepositoryInterface $productAttributeRepository, |
||
| 39 | string $attributeCode, |
||
| 40 | LocaleContextInterface $localeContext |
||
| 41 | ) { |
||
| 42 | $this->attributeNameResolver = $attributeNameResolver; |
||
| 43 | $this->productAttributeRepository = $productAttributeRepository; |
||
| 44 | $this->attributeCode = $attributeCode; |
||
| 45 | $this->localeContext = $localeContext; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getAggregation(): AbstractAggregation |
||
| 49 | { |
||
| 50 | $aggregation = new Terms(''); |
||
| 51 | $aggregation->setField($this->getFieldName()); |
||
| 52 | |||
| 53 | return $aggregation; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function getQuery(array $selectedBuckets): AbstractQuery |
||
| 57 | { |
||
| 58 | return new TermsQuery($this->getFieldName(), $selectedBuckets); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function getBucketLabel(array $bucket): string |
||
| 62 | { |
||
| 63 | $label = ucwords(str_replace('_', ' ', $bucket['key'])); |
||
| 64 | |||
| 65 | return sprintf('%s (%s)', $label, $bucket['doc_count']); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getLabel(): string |
||
| 69 | { |
||
| 70 | return $this->getProductAttribute()->getName(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 71 | } |
||
| 72 | |||
| 73 | private function getFieldName(): string |
||
| 74 | { |
||
| 75 | return \sprintf( |
||
| 76 | '%s_%s.keyword', |
||
| 77 | $this->attributeNameResolver->resolvePropertyName($this->attributeCode), |
||
| 78 | $this->localeContext->getLocaleCode() |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | private function getProductAttribute(): AttributeInterface |
||
| 83 | { |
||
| 84 | $attribute = $this->productAttributeRepository->findOneBy(['code' => $this->attributeCode]); |
||
| 85 | if (!$attribute instanceof AttributeInterface) { |
||
| 86 | throw new \RuntimeException(sprintf('Cannot find attribute with code "%s"', $this->attributeCode)); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $attribute; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |