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

OptionFacet   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 51
rs 10
c 1
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabel() 0 7 2
A getFieldName() 0 3 1
A getQuery() 0 3 1
A getBucketLabel() 0 4 1
A __construct() 0 8 1
A getAggregation() 0 5 1
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\Terms;
10
use Elastica\Query\AbstractQuery;
11
use Elastica\Query\Terms as TermsQuery;
12
use Sylius\Component\Product\Model\ProductOptionInterface;
13
use Sylius\Component\Resource\Repository\RepositoryInterface;
14
15
final class OptionFacet implements FacetInterface
16
{
17
    /** @var ConcatedNameResolverInterface */
18
    private $optionNameResolver;
19
20
    /** @var RepositoryInterface */
21
    private $productOptionRepository;
22
23
    /** @var string */
24
    private $productOptionCode;
25
26
    public function __construct(
27
        ConcatedNameResolverInterface $optionNameResolver,
28
        RepositoryInterface $productOptionRepository,
29
        string $optionCode
30
    ) {
31
        $this->optionNameResolver = $optionNameResolver;
32
        $this->productOptionRepository = $productOptionRepository;
33
        $this->productOptionCode = $optionCode;
34
    }
35
36
    public function getAggregation(): AbstractAggregation
37
    {
38
        $aggregation = new Terms('');
39
        $aggregation->setField($this->getFieldName());
40
        return $aggregation;
41
    }
42
43
    public function getQuery(array $selectedBuckets): AbstractQuery
44
    {
45
        return new TermsQuery($this->getFieldName(), $selectedBuckets);
46
    }
47
48
    public function getBucketLabel(array $bucket): string
49
    {
50
        $label = ucwords(str_replace('_', ' ', $bucket['key']));
51
        return sprintf('%s (%s)', $label, $bucket['doc_count']);
52
    }
53
54
    public function getLabel(): string
55
    {
56
        $productOption = $this->productOptionRepository->findOneBy(['code' => $this->productOptionCode]);
57
        if (!$productOption instanceof ProductOptionInterface) {
58
            throw new \RuntimeException(sprintf('Cannot find product option with code "%s"', $this->productOptionCode));
59
        }
60
        return $productOption->getName();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $productOption->getName() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
61
    }
62
63
    private function getFieldName(): string
64
    {
65
        return $this->optionNameResolver->resolvePropertyName($this->productOptionCode) . '.keyword';
66
    }
67
}
68