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\Product\Model\ProductOptionInterface; |
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
20
|
|
|
|
21
|
|
|
final class OptionFacet implements FacetInterface |
22
|
|
|
{ |
23
|
|
|
/** @var ConcatedNameResolverInterface */ |
24
|
|
|
private $optionNameResolver; |
25
|
|
|
|
26
|
|
|
/** @var RepositoryInterface */ |
27
|
|
|
private $productOptionRepository; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $productOptionCode; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
ConcatedNameResolverInterface $optionNameResolver, |
34
|
|
|
RepositoryInterface $productOptionRepository, |
35
|
|
|
string $optionCode |
36
|
|
|
) { |
37
|
|
|
$this->optionNameResolver = $optionNameResolver; |
38
|
|
|
$this->productOptionRepository = $productOptionRepository; |
39
|
|
|
$this->productOptionCode = $optionCode; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getAggregation(): AbstractAggregation |
43
|
|
|
{ |
44
|
|
|
$aggregation = new Terms(''); |
45
|
|
|
$aggregation->setField($this->getFieldName()); |
46
|
|
|
|
47
|
|
|
return $aggregation; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getQuery(array $selectedBuckets): AbstractQuery |
51
|
|
|
{ |
52
|
|
|
return new TermsQuery($this->getFieldName(), $selectedBuckets); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getBucketLabel(array $bucket): string |
56
|
|
|
{ |
57
|
|
|
$label = ucwords(str_replace('_', ' ', $bucket['key'])); |
58
|
|
|
|
59
|
|
|
return sprintf('%s (%s)', $label, $bucket['doc_count']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getLabel(): string |
63
|
|
|
{ |
64
|
|
|
$productOption = $this->productOptionRepository->findOneBy(['code' => $this->productOptionCode]); |
65
|
|
|
if (!$productOption instanceof ProductOptionInterface) { |
66
|
|
|
throw new \RuntimeException(sprintf('Cannot find product option with code "%s"', $this->productOptionCode)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $productOption->getName(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getFieldName(): string |
73
|
|
|
{ |
74
|
|
|
return $this->optionNameResolver->resolvePropertyName($this->productOptionCode) . '.keyword'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|