1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\Facet; |
5
|
|
|
|
6
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
7
|
|
|
use Elastica\Aggregation\AbstractAggregation; |
8
|
|
|
use Elastica\Aggregation\Terms; |
9
|
|
|
use Elastica\Query\AbstractQuery; |
10
|
|
|
use Elastica\Query\Terms as TermsQuery; |
11
|
|
|
use Sylius\Component\Attribute\Model\AttributeInterface; |
12
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
13
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
14
|
|
|
|
15
|
|
|
final class AttributeFacet implements FacetInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ConcatedNameResolverInterface |
19
|
|
|
*/ |
20
|
|
|
private $attributeNameResolver; |
21
|
|
|
/** |
22
|
|
|
* @var RepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $productAttributeRepository; |
25
|
|
|
/** |
26
|
|
|
* @var LocaleContextInterface |
27
|
|
|
*/ |
28
|
|
|
private $localeContext; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $attributeCode; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ConcatedNameResolverInterface $attributeNameResolver, |
36
|
|
|
RepositoryInterface $productAttributeRepository, |
37
|
|
|
LocaleContextInterface $localeContext, |
38
|
|
|
string $attributeCode |
39
|
|
|
) { |
40
|
|
|
$this->attributeNameResolver = $attributeNameResolver; |
41
|
|
|
$this->productAttributeRepository = $productAttributeRepository; |
42
|
|
|
$this->localeContext = $localeContext; |
43
|
|
|
$this->attributeCode = $attributeCode; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getAggregation(): AbstractAggregation |
47
|
|
|
{ |
48
|
|
|
$aggregation = new Terms(''); |
49
|
|
|
$aggregation->setField($this->getFieldName()); |
50
|
|
|
return $aggregation; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getQuery(array $selectedBuckets): AbstractQuery |
54
|
|
|
{ |
55
|
|
|
return new TermsQuery($this->getFieldName(), $selectedBuckets); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getBucketLabel(array $bucket): string |
59
|
|
|
{ |
60
|
|
|
$attribute = $this->getProductAttribute(); |
61
|
|
|
$label = $value = $bucket['key']; |
62
|
|
|
if ($attribute->getType() === 'select') { |
63
|
|
|
$configuration = $attribute->getConfiguration(); |
64
|
|
|
if (isset($configuration['choices'][$value][$this->localeContext->getLocaleCode()])) { |
65
|
|
|
$label = $configuration['choices'][$value][$this->localeContext->getLocaleCode()]; |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
$label = ucwords(str_replace('_', ' ', $label)); |
69
|
|
|
} |
70
|
|
|
return sprintf('%s (%s)', $label, $bucket['doc_count']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getLabel(): string |
74
|
|
|
{ |
75
|
|
|
return $this->getProductAttribute()->getName(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function getFieldName(): string |
82
|
|
|
{ |
83
|
|
|
return $this->attributeNameResolver->resolvePropertyName($this->attributeCode) . '.keyword'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return AttributeInterface |
88
|
|
|
*/ |
89
|
|
|
private function getProductAttribute(): AttributeInterface |
90
|
|
|
{ |
91
|
|
|
$attribute = $this->productAttributeRepository->findOneBy(['code' => $this->attributeCode]); |
92
|
|
|
if (!$attribute instanceof AttributeInterface) { |
93
|
|
|
throw new \RuntimeException(sprintf('Cannot find attribute with code "%s"', $this->attributeCode)); |
94
|
|
|
} |
95
|
|
|
return $attribute; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|