Passed
Pull Request — master (#69)
by Manuele
04:01
created

PriceFacet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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