Passed
Push — master ( cc4e78...1262d4 )
by Damian
04:08
created

PriceFacet::getBucketLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusElasticsearchPlugin\Facet;
6
7
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
8
use Elastica\Aggregation\AbstractAggregation;
9
use Elastica\Aggregation\Histogram;
10
use Elastica\Query\AbstractQuery;
11
use Elastica\Query\BoolQuery;
12
use Elastica\Query\Range;
13
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
14
use Sylius\Component\Core\Context\ShopperContextInterface;
15
16
final class PriceFacet implements FacetInterface
17
{
18
    public const FACET_ID = 'price';
19
20
    /** @var ConcatedNameResolverInterface */
21
    private $channelPricingNameResolver;
22
23
    /** @var MoneyFormatterInterface */
24
    private $moneyFormatter;
25
26
    /** @var ShopperContextInterface */
27
    private $shopperContext;
28
29
    /** @var int */
30
    private $interval;
31
32
    public function __construct(
33
        ConcatedNameResolverInterface $channelPricingNameResolver,
34
        MoneyFormatterInterface $moneyFormatter,
35
        ShopperContextInterface $shopperContext,
36
        int $interval
37
    ) {
38
        $this->channelPricingNameResolver = $channelPricingNameResolver;
39
        $this->moneyFormatter = $moneyFormatter;
40
        $this->interval = $interval;
41
        $this->shopperContext = $shopperContext;
42
    }
43
44
    public function getAggregation(): AbstractAggregation
45
    {
46
        $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
47
            $this->shopperContext->getChannel()->getCode()
0 ignored issues
show
Bug introduced by
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

47
            /** @scrutinizer ignore-type */ $this->shopperContext->getChannel()->getCode()
Loading history...
48
        );
49
        $histogram = new Histogram(self::FACET_ID, $priceFieldName, $this->interval);
50
        $histogram->setMinimumDocumentCount(1);
51
        return $histogram;
52
    }
53
54
    public function getQuery(array $selectedBuckets): AbstractQuery
55
    {
56
        $priceFieldName = $this->channelPricingNameResolver->resolvePropertyName(
57
            $this->shopperContext->getChannel()->getCode()
0 ignored issues
show
Bug introduced by
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

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