-
Notifications
You must be signed in to change notification settings - Fork 92
/
PriceFacet.php
82 lines (69 loc) · 2.64 KB
/
PriceFacet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/
declare(strict_types=1);
namespace BitBag\SyliusElasticsearchPlugin\Facet;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Aggregation\AbstractAggregation;
use Elastica\Aggregation\Histogram;
use Elastica\Query\AbstractQuery;
use Elastica\Query\BoolQuery;
use Elastica\Query\Range;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Component\Core\Context\ShopperContextInterface;
final class PriceFacet implements FacetInterface
{
public const FACET_ID = 'price';
public function __construct(
private ConcatedNameResolverInterface $channelPricingNameResolver,
private MoneyFormatterInterface $moneyFormatter,
private ShopperContextInterface $shopperContext,
private int $interval
) {
}
public function getAggregation(): AbstractAggregation
{
$priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
(string) $this->shopperContext->getChannel()->getCode()
);
$histogram = new Histogram(self::FACET_ID, $priceFieldName, $this->interval);
$histogram->setMinimumDocumentCount(1);
return $histogram;
}
public function getQuery(array $selectedBuckets): AbstractQuery
{
$priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
(string) $this->shopperContext->getChannel()->getCode()
);
$query = new BoolQuery();
foreach ($selectedBuckets as $selectedBucket) {
$query->addShould(
new Range($priceFieldName, ['gte' => $selectedBucket, 'lte' => $selectedBucket + $this->interval])
);
}
return $query;
}
public function getBucketLabel(array $bucket): string
{
$from = $this->moneyFormatter->format(
(int) $bucket['key'],
$this->shopperContext->getCurrencyCode(),
$this->shopperContext->getLocaleCode()
);
$to = $this->moneyFormatter->format(
(int) ($bucket['key'] + $this->interval),
$this->shopperContext->getCurrencyCode(),
$this->shopperContext->getLocaleCode()
);
return sprintf('%s - %s (%s)', $from, $to, $bucket['doc_count']);
}
public function getLabel(): string
{
return 'bitbag_sylius_elasticsearch_plugin.ui.facet.price.label';
}
}