1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\Facet; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
8
|
|
|
use Elastica\Aggregation\AbstractAggregation; |
9
|
|
|
use Elastica\Aggregation\Terms; |
10
|
|
|
use Elastica\Query\AbstractQuery; |
11
|
|
|
use Elastica\Query\Terms as TermsQuery; |
12
|
|
|
use Sylius\Component\Product\Model\ProductOptionInterface; |
13
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
14
|
|
|
|
15
|
|
|
final class OptionFacet implements FacetInterface |
16
|
|
|
{ |
17
|
|
|
/** @var ConcatedNameResolverInterface */ |
18
|
|
|
private $optionNameResolver; |
19
|
|
|
|
20
|
|
|
/** @var RepositoryInterface */ |
21
|
|
|
private $productOptionRepository; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $productOptionCode; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
ConcatedNameResolverInterface $optionNameResolver, |
28
|
|
|
RepositoryInterface $productOptionRepository, |
29
|
|
|
string $optionCode |
30
|
|
|
) { |
31
|
|
|
$this->optionNameResolver = $optionNameResolver; |
32
|
|
|
$this->productOptionRepository = $productOptionRepository; |
33
|
|
|
$this->productOptionCode = $optionCode; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getAggregation(): AbstractAggregation |
37
|
|
|
{ |
38
|
|
|
$aggregation = new Terms(''); |
39
|
|
|
$aggregation->setField($this->getFieldName()); |
40
|
|
|
return $aggregation; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getQuery(array $selectedBuckets): AbstractQuery |
44
|
|
|
{ |
45
|
|
|
return new TermsQuery($this->getFieldName(), $selectedBuckets); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getBucketLabel(array $bucket): string |
49
|
|
|
{ |
50
|
|
|
$label = ucwords(str_replace('_', ' ', $bucket['key'])); |
51
|
|
|
return sprintf('%s (%s)', $label, $bucket['doc_count']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getLabel(): string |
55
|
|
|
{ |
56
|
|
|
$productOption = $this->productOptionRepository->findOneBy(['code' => $this->productOptionCode]); |
57
|
|
|
if (!$productOption instanceof ProductOptionInterface) { |
58
|
|
|
throw new \RuntimeException(sprintf('Cannot find product option with code "%s"', $this->productOptionCode)); |
59
|
|
|
} |
60
|
|
|
return $productOption->getName(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getFieldName(): string |
64
|
|
|
{ |
65
|
|
|
return $this->optionNameResolver->resolvePropertyName($this->productOptionCode) . '.keyword'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|