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