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\Histogram; |
9
|
|
|
use Elastica\Query\AbstractQuery; |
10
|
|
|
use Elastica\Query\BoolQuery; |
11
|
|
|
use Elastica\Query\Range; |
12
|
|
|
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface; |
13
|
|
|
use Sylius\Component\Core\Context\ShopperContextInterface; |
14
|
|
|
|
15
|
|
|
final class PriceFacet implements FacetInterface |
16
|
|
|
{ |
17
|
|
|
public const FACET_ID = 'price'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ConcatedNameResolverInterface |
21
|
|
|
*/ |
22
|
|
|
private $channelPricingNameResolver; |
23
|
|
|
/** |
24
|
|
|
* @var MoneyFormatterInterface |
25
|
|
|
*/ |
26
|
|
|
private $moneyFormatter; |
27
|
|
|
/** |
28
|
|
|
* @var ShopperContextInterface |
29
|
|
|
*/ |
30
|
|
|
private $shopperContext; |
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $interval; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
ConcatedNameResolverInterface $channelPricingNameResolver, |
38
|
|
|
MoneyFormatterInterface $moneyFormatter, |
39
|
|
|
ShopperContextInterface $shopperContext, |
40
|
|
|
int $interval |
41
|
|
|
) { |
42
|
|
|
$this->channelPricingNameResolver = $channelPricingNameResolver; |
43
|
|
|
$this->moneyFormatter = $moneyFormatter; |
44
|
|
|
$this->interval = $interval; |
45
|
|
|
$this->shopperContext = $shopperContext; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getAggregation(): AbstractAggregation |
49
|
|
|
{ |
50
|
|
|
$priceFieldName = $this->channelPricingNameResolver->resolvePropertyName( |
51
|
|
|
$this->shopperContext->getChannel()->getCode() |
|
|
|
|
52
|
|
|
); |
53
|
|
|
$histogram = new Histogram(self::FACET_ID, $priceFieldName, $this->interval); |
54
|
|
|
$histogram->setMinimumDocumentCount(1); |
55
|
|
|
return $histogram; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getQuery(array $selectedBuckets): AbstractQuery |
59
|
|
|
{ |
60
|
|
|
$priceFieldName = $this->channelPricingNameResolver->resolvePropertyName( |
61
|
|
|
$this->shopperContext->getChannel()->getCode() |
|
|
|
|
62
|
|
|
); |
63
|
|
|
$query = new BoolQuery(); |
64
|
|
|
foreach ($selectedBuckets as $selectedBucket) { |
65
|
|
|
$query->addShould( |
66
|
|
|
new Range($priceFieldName, ['gte' => $selectedBucket, 'lte' => $selectedBucket + $this->interval]) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
return $query; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getBucketLabel(array $bucket): string |
73
|
|
|
{ |
74
|
|
|
$from = $this->moneyFormatter->format( |
75
|
|
|
(int)$bucket['key'], |
76
|
|
|
$this->shopperContext->getCurrencyCode(), |
77
|
|
|
$this->shopperContext->getLocaleCode() |
78
|
|
|
); |
79
|
|
|
$to = $this->moneyFormatter->format( |
80
|
|
|
(int)($bucket['key'] + $this->interval), |
81
|
|
|
$this->shopperContext->getCurrencyCode(), |
82
|
|
|
$this->shopperContext->getLocaleCode() |
83
|
|
|
); |
84
|
|
|
return sprintf('%s - %s (%s)', $from, $to, $bucket['doc_count']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getLabel(): string |
88
|
|
|
{ |
89
|
|
|
return 'bitbag_sylius_elasticsearch_plugin.ui.facet.price.label'; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|