Issues (36)

src/Facet/PriceFacet.php (2 issues)

Labels
Severity
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\Facet;
12
13
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
14
use Elastica\Aggregation\AbstractAggregation;
15
use Elastica\Aggregation\Histogram;
16
use Elastica\Query\AbstractQuery;
17
use Elastica\Query\BoolQuery;
18
use Elastica\Query\Range;
19
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
20
use Sylius\Component\Core\Context\ShopperContextInterface;
21
22
final class PriceFacet implements FacetInterface
23
{
24
    public const FACET_ID = 'price';
25
26
    /** @var ConcatedNameResolverInterface */
27
    private $channelPricingNameResolver;
28
29
    /** @var MoneyFormatterInterface */
30
    private $moneyFormatter;
31
32
    /** @var ShopperContextInterface */
33
    private $shopperContext;
34
35
    /** @var int */
36
    private $interval;
37
38
    public function __construct(
39
        ConcatedNameResolverInterface $channelPricingNameResolver,
40
        MoneyFormatterInterface $moneyFormatter,
41
        ShopperContextInterface $shopperContext,
42
        int $interval
43
    ) {
44
        $this->channelPricingNameResolver = $channelPricingNameResolver;
45
        $this->moneyFormatter = $moneyFormatter;
46
        $this->interval = $interval;
47
        $this->shopperContext = $shopperContext;
48
    }
49
50
    public function getAggregation(): AbstractAggregation
51
    {
52
        $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
53
            $this->shopperContext->getChannel()->getCode()
0 ignored issues
show
It seems like $this->shopperContext->getChannel()->getCode() 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

53
            /** @scrutinizer ignore-type */ $this->shopperContext->getChannel()->getCode()
Loading history...
54
        );
55
        $histogram = new Histogram(self::FACET_ID, $priceFieldName, $this->interval);
56
        $histogram->setMinimumDocumentCount(1);
57
58
        return $histogram;
59
    }
60
61
    public function getQuery(array $selectedBuckets): AbstractQuery
62
    {
63
        $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
64
            $this->shopperContext->getChannel()->getCode()
0 ignored issues
show
It seems like $this->shopperContext->getChannel()->getCode() 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

64
            /** @scrutinizer ignore-type */ $this->shopperContext->getChannel()->getCode()
Loading history...
65
        );
66
        $query = new BoolQuery();
67
        foreach ($selectedBuckets as $selectedBucket) {
68
            $query->addShould(
69
                new Range($priceFieldName, ['gte' => $selectedBucket, 'lte' => $selectedBucket + $this->interval])
70
            );
71
        }
72
73
        return $query;
74
    }
75
76
    public function getBucketLabel(array $bucket): string
77
    {
78
        $from = $this->moneyFormatter->format(
79
            (int) $bucket['key'],
80
            $this->shopperContext->getCurrencyCode(),
81
            $this->shopperContext->getLocaleCode()
82
        );
83
        $to = $this->moneyFormatter->format(
84
            (int) ($bucket['key'] + $this->interval),
85
            $this->shopperContext->getCurrencyCode(),
86
            $this->shopperContext->getLocaleCode()
87
        );
88
89
        return sprintf('%s - %s (%s)', $from, $to, $bucket['doc_count']);
90
    }
91
92
    public function getLabel(): string
93
    {
94
        return 'bitbag_sylius_elasticsearch_plugin.ui.facet.price.label';
95
    }
96
}
97