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

AttributeFacet   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProductAttribute() 0 7 2
A getBucketLabel() 0 4 1
A getLabel() 0 3 1
A __construct() 0 8 1
A getAggregation() 0 5 1
A getFieldName() 0 3 1
A getQuery() 0 3 1
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\Terms;
9
use Elastica\Query\AbstractQuery;
10
use Elastica\Query\Terms as TermsQuery;
11
use Sylius\Component\Attribute\Model\AttributeInterface;
12
use Sylius\Component\Resource\Repository\RepositoryInterface;
13
14
final class AttributeFacet implements FacetInterface
15
{
16
    /**
17
     * @var ConcatedNameResolverInterface
18
     */
19
    private $attributeNameResolver;
20
    /**
21
     * @var RepositoryInterface
22
     */
23
    private $productAttributeRepository;
24
    /**
25
     * @var string
26
     */
27
    private $attributeCode;
28
29
    public function __construct(
30
        ConcatedNameResolverInterface $attributeNameResolver,
31
        RepositoryInterface $productAttributeRepository,
32
        string $attributeCode
33
    ) {
34
        $this->attributeNameResolver = $attributeNameResolver;
35
        $this->productAttributeRepository = $productAttributeRepository;
36
        $this->attributeCode = $attributeCode;
37
    }
38
39
    public function getAggregation(): AbstractAggregation
40
    {
41
        $aggregation = new Terms('');
42
        $aggregation->setField($this->getFieldName());
43
        return $aggregation;
44
    }
45
46
    public function getQuery(array $selectedBuckets): AbstractQuery
47
    {
48
        return new TermsQuery($this->getFieldName(), $selectedBuckets);
49
    }
50
51
    public function getBucketLabel(array $bucket): string
52
    {
53
        $label = ucwords(str_replace('_', ' ', $bucket['key']));
54
        return sprintf('%s (%s)', $label, $bucket['doc_count']);
55
    }
56
57
    public function getLabel(): string
58
    {
59
        return $this->getProductAttribute()->getName();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getProductAttribute()->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...
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    private function getFieldName(): string
66
    {
67
        return $this->attributeNameResolver->resolvePropertyName($this->attributeCode) . '.keyword';
68
    }
69
70
    /**
71
     * @return AttributeInterface
72
     */
73
    private function getProductAttribute(): AttributeInterface
74
    {
75
        $attribute = $this->productAttributeRepository->findOneBy(['code' => $this->attributeCode]);
76
        if (!$attribute instanceof AttributeInterface) {
77
            throw new \RuntimeException(sprintf('Cannot find attribute with code "%s"', $this->attributeCode));
78
        }
79
        return $attribute;
80
    }
81
}
82