Completed
Push — master ( 8f058a...ace437 )
by
unknown
15s queued 11s
created

HasPriceBetweenQueryBuilder::getDataByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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);
0 ignored issues
show
Bug introduced by
It seems like $channelCode can also be of type null; however, parameter $suffix of BitBag\SyliusElasticsear...::resolvePropertyName() 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 ignore-type  annotation

65
        $propertyName = $this->channelPricingNameResolver->resolvePropertyName(/** @scrutinizer ignore-type */ $channelCode);
Loading history...
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);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

88
            $price = $this->currencyConverter->convert($price, $currentCurrencyCode, /** @scrutinizer ignore-type */ $channelBaseCurrencyCode);
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
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 ignore-deprecated  annotation

96
        $transformer = new SyliusMoneyTransformer(2, false, /** @scrutinizer ignore-deprecated */ SyliusMoneyTransformer::ROUND_HALF_UP, 100);

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.

Loading history...
97
98
        return $transformer->reverseTransform($price);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transformer->reverseTransform($price) returns the type null which is incompatible with the type-hinted return integer.
Loading history...
Bug introduced by
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 getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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