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\QueryBuilder; |
||||
12 | |||||
13 | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface; |
||||
14 | use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\PriceNameResolverInterface; |
||||
15 | use Elastica\Query\AbstractQuery; |
||||
16 | use Elastica\Query\Range; |
||||
17 | use Sylius\Bundle\MoneyBundle\Form\DataTransformer\SyliusMoneyTransformer; |
||||
18 | use Sylius\Component\Channel\Context\ChannelContextInterface; |
||||
19 | use Sylius\Component\Core\Model\ChannelInterface; |
||||
20 | use Sylius\Component\Currency\Context\CurrencyContextInterface; |
||||
21 | use Sylius\Component\Currency\Converter\CurrencyConverterInterface; |
||||
22 | |||||
23 | final class HasPriceBetweenQueryBuilder implements QueryBuilderInterface |
||||
24 | { |
||||
25 | /** @var ConcatedNameResolverInterface */ |
||||
26 | private $channelPricingNameResolver; |
||||
27 | |||||
28 | /** @var PriceNameResolverInterface */ |
||||
29 | private $priceNameResolver; |
||||
30 | |||||
31 | /** @var ChannelContextInterface */ |
||||
32 | private $channelContext; |
||||
33 | |||||
34 | /** @var CurrencyContextInterface */ |
||||
35 | private $currencyContext; |
||||
36 | |||||
37 | /** @var CurrencyConverterInterface */ |
||||
38 | private $currencyConverter; |
||||
39 | |||||
40 | public function __construct( |
||||
41 | PriceNameResolverInterface $priceNameResolver, |
||||
42 | ConcatedNameResolverInterface $channelPricingNameResolver, |
||||
43 | ChannelContextInterface $channelContext, |
||||
44 | CurrencyContextInterface $currencyContext, |
||||
45 | CurrencyConverterInterface $currencyConverter |
||||
46 | ) { |
||||
47 | $this->channelPricingNameResolver = $channelPricingNameResolver; |
||||
48 | $this->priceNameResolver = $priceNameResolver; |
||||
49 | $this->channelContext = $channelContext; |
||||
50 | $this->currencyContext = $currencyContext; |
||||
51 | $this->currencyConverter = $currencyConverter; |
||||
52 | } |
||||
53 | |||||
54 | public function buildQuery(array $data): ?AbstractQuery |
||||
55 | { |
||||
56 | $dataMinPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMinPriceName()); |
||||
57 | $dataMaxPrice = $this->getDataByKey($data, $this->priceNameResolver->resolveMaxPriceName()); |
||||
58 | |||||
59 | $minPrice = $dataMinPrice ? $this->resolveBasePrice($dataMinPrice) : null; |
||||
60 | $maxPrice = $dataMaxPrice ? $this->resolveBasePrice($dataMaxPrice) : null; |
||||
61 | |||||
62 | $channelCode = $this->channelContext->getChannel()->getCode(); |
||||
63 | $propertyName = $this->channelPricingNameResolver->resolvePropertyName($channelCode); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
64 | $rangeQuery = new Range(); |
||||
65 | |||||
66 | $paramValue = $this->getQueryParamValue($minPrice, $maxPrice); |
||||
67 | |||||
68 | if (null === $paramValue) { |
||||
69 | return null; |
||||
70 | } |
||||
71 | |||||
72 | $rangeQuery->setParam($propertyName, $paramValue); |
||||
73 | |||||
74 | return $rangeQuery; |
||||
75 | } |
||||
76 | |||||
77 | private function resolveBasePrice(string $price): int |
||||
78 | { |
||||
79 | $price = $this->convertFromString($price); |
||||
80 | /** @var ChannelInterface $channel */ |
||||
81 | $channel = $this->channelContext->getChannel(); |
||||
82 | $currentCurrencyCode = $this->currencyContext->getCurrencyCode(); |
||||
83 | $channelBaseCurrencyCode = $channel->getBaseCurrency()->getCode(); |
||||
84 | |||||
85 | if ($currentCurrencyCode !== $channelBaseCurrencyCode) { |
||||
86 | $price = $this->currencyConverter->convert($price, $currentCurrencyCode, $channelBaseCurrencyCode); |
||||
0 ignored issues
–
show
It seems like
$channelBaseCurrencyCode can also be of type null ; however, parameter $targetCurrencyCode of Sylius\Component\Currenc...terInterface::convert() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
87 | } |
||||
88 | |||||
89 | return $price; |
||||
90 | } |
||||
91 | |||||
92 | private function convertFromString(string $price): int |
||||
93 | { |
||||
94 | $transformer = new SyliusMoneyTransformer(2, false, SyliusMoneyTransformer::ROUND_HALF_UP, 100); |
||||
0 ignored issues
–
show
The constant
Symfony\Component\Form\E...nsformer::ROUND_HALF_UP has been deprecated: since Symfony 5.1, use \NumberFormatter::ROUND_HALFUP instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This class constant has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead. ![]() |
|||||
95 | |||||
96 | return $transformer->reverseTransform($price); |
||||
0 ignored issues
–
show
Are you sure the usage of
$transformer->reverseTransform($price) targeting Sylius\Bundle\MoneyBundl...mer::reverseTransform() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
97 | } |
||||
98 | |||||
99 | private function getDataByKey(array $data, ?string $key = null): ?string |
||||
100 | { |
||||
101 | return $data[$key] ?? null; |
||||
102 | } |
||||
103 | |||||
104 | private function getQueryParamValue(?int $min, ?int $max): ?array |
||||
105 | { |
||||
106 | foreach (['gte' => $min, 'lte' => $max] as $key => $value) { |
||||
107 | if (null !== $value) { |
||||
108 | $paramValue[$key] = $value; |
||||
109 | } |
||||
110 | } |
||||
111 | |||||
112 | return $paramValue ?? null; |
||||
113 | } |
||||
114 | } |
||||
115 |