Passed
Push — sheepy/elevation-configuration ( bdeab0...bcf7bc )
by Marco
18:34
created

QueryComponentBoostTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 25
c 4
b 0
f 0
dl 0
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBoostTerms() 0 3 1
A setBoostTerms() 0 5 1
A buildBoosts() 0 14 4
A buildQueryBoost() 0 11 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Traits;
5
6
use Firesphere\SolrSearch\Factories\QueryComponentFactory;
7
use Firesphere\SolrSearch\Queries\BaseQuery;
8
use Minimalcode\Search\Criteria;
9
10
trait QueryComponentBoostTrait
11
{
12
    /**
13
     * @var BaseQuery
14
     */
15
    protected $query;
16
    /**
17
     * @var array
18
     */
19
    protected $boostTerms = [];
20
    /**
21
     * @var array
22
     */
23
    protected $queryArray = [];
24
25
    /**
26
     * @return array
27
     */
28
    public function getBoostTerms(): array
29
    {
30
        return $this->boostTerms;
31
    }
32
33
    /**
34
     * @param array $boostTerms
35
     * @return QueryComponentFactory
36
     */
37
    public function setBoostTerms(array $boostTerms): self
38
    {
39
        $this->boostTerms = $boostTerms;
40
41
        return $this;
42
    }
43
44
    /**
45
     * Add the index-time boosting to the query
46
     */
47
    protected function buildBoosts(): void
48
    {
49
        $boostedFields = $this->query->getBoostedFields();
50
        $queries = $this->getQueryArray();
0 ignored issues
show
Bug introduced by
It seems like getQueryArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        /** @scrutinizer ignore-call */ 
51
        $queries = $this->getQueryArray();
Loading history...
51
        foreach ($boostedFields as $field => $boost) {
52
            $terms = [];
53
            foreach ($queries as $term) {
54
                $terms[] = $term;
55
            }
56
            if (count($terms)) {
57
                $booster = Criteria::where(str_replace('.', '_', $field))
58
                    ->in($terms)
59
                    ->boost($boost);
60
                $this->queryArray[] = $booster->getQuery();
61
            }
62
        }
63
    }
64
65
    /**
66
     * Set boosting at Query time
67
     *
68
     * @param array $search
69
     * @param string $term
70
     * @param array $boostTerms
71
     * @return array
72
     */
73
    protected function buildQueryBoost($search, string $term, array &$boostTerms): array
74
    {
75
        foreach ($search['fields'] as $boostField) {
76
            $boostField = str_replace('.', '_', $boostField);
77
            $criteria = Criteria::where($boostField)
78
                ->is($term)
79
                ->boost($search['boost']);
80
            $boostTerms[] = $criteria->getQuery();
81
        }
82
83
        return $boostTerms;
84
    }
85
}
86