1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusElasticsearchPlugin\QueryBuilder; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
16
|
|
|
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\PriceNameResolverInterface; |
17
|
|
|
use Elastica\Query\AbstractQuery; |
18
|
|
|
use Elastica\Query\Range; |
19
|
|
|
use Sylius\Bundle\MoneyBundle\Form\DataTransformer\SyliusMoneyTransformer; |
20
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
21
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
22
|
|
|
use Sylius\Component\Currency\Context\CurrencyContextInterface; |
23
|
|
|
use Sylius\Component\Currency\Converter\CurrencyConverterInterface; |
24
|
|
|
|
25
|
|
|
final class HasPriceBetweenQueryBuilder implements QueryBuilderInterface |
26
|
|
|
{ |
27
|
|
|
/** @var ConcatedNameResolverInterface */ |
28
|
|
|
private $channelPricingNameResolver; |
29
|
|
|
|
30
|
|
|
/** @var PriceNameResolverInterface */ |
31
|
|
|
private $priceNameResolver; |
32
|
|
|
|
33
|
|
|
/** @var ChannelContextInterface */ |
34
|
|
|
private $channelContext; |
35
|
|
|
|
36
|
|
|
/** @var CurrencyContextInterface */ |
37
|
|
|
private $currencyContext; |
38
|
|
|
|
39
|
|
|
/** @var CurrencyConverterInterface */ |
40
|
|
|
private $currencyConverter; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
PriceNameResolverInterface $priceNameResolver, |
44
|
|
|
ConcatedNameResolverInterface $channelPricingNameResolver, |
45
|
|
|
ChannelContextInterface $channelContext, |
46
|
|
|
CurrencyContextInterface $currencyContext, |
47
|
|
|
CurrencyConverterInterface $currencyConverter |
48
|
|
|
) { |
49
|
|
|
$this->channelPricingNameResolver = $channelPricingNameResolver; |
50
|
|
|
$this->priceNameResolver = $priceNameResolver; |
51
|
|
|
$this->channelContext = $channelContext; |
52
|
|
|
$this->currencyContext = $currencyContext; |
53
|
|
|
$this->currencyConverter = $currencyConverter; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function buildQuery(array $data): ?AbstractQuery |
57
|
|
|
{ |
58
|
|
|
$dataMinPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMinPriceName()); |
59
|
|
|
$dataMaxPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMaxPriceName()); |
60
|
|
|
|
61
|
|
|
$minPrice = $dataMinPrice ? $this->resolveBasePrice($dataMinPrice) : null; |
62
|
|
|
$maxPrice = $dataMaxPrice ? $this->resolveBasePrice($dataMaxPrice) : null; |
63
|
|
|
|
64
|
|
|
$channelCode = $this->channelContext->getChannel()->getCode(); |
65
|
|
|
$propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelCode); |
|
|
|
|
66
|
|
|
$rangeQuery = new Range(); |
67
|
|
|
|
68
|
|
|
$paramValue = $this->getQueryParamValue($minPrice , $maxPrice); |
69
|
|
|
|
70
|
|
|
if (null === $paramValue) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$rangeQuery->setParam($propertyName, $paramValue); |
75
|
|
|
|
76
|
|
|
return $rangeQuery; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function resolveBasePrice(string $price): int |
80
|
|
|
{ |
81
|
|
|
$price = $this->convertFromString($price); |
82
|
|
|
/** @var ChannelInterface $channel */ |
83
|
|
|
$channel = $this->channelContext->getChannel(); |
84
|
|
|
$currentCurrencyCode = $this->currencyContext->getCurrencyCode(); |
85
|
|
|
$channelBaseCurrencyCode = $channel->getBaseCurrency()->getCode(); |
86
|
|
|
|
87
|
|
|
if ($currentCurrencyCode !== $channelBaseCurrencyCode) { |
88
|
|
|
$price = $this->currencyConverter->convert($price, $currentCurrencyCode, $channelBaseCurrencyCode); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $price; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function convertFromString(string $price): int |
95
|
|
|
{ |
96
|
|
|
$transformer = new SyliusMoneyTransformer(2, false, SyliusMoneyTransformer::ROUND_HALF_UP, 100); |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $transformer->reverseTransform($price); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function getDataByKey(array $data, ?string $key = null): ?string |
102
|
|
|
{ |
103
|
|
|
return $data[$key] ?? null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getQueryParamValue(?int $min, ?int $max): ?array |
107
|
|
|
{ |
108
|
|
|
foreach (['gte' => $min, 'lte' => $max] as $key => $value) { |
109
|
|
|
if (null !== $value) { |
110
|
|
|
$paramValue[$key] = $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $paramValue ?? null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|