Passed
Push — sheepy/introspection ( 000d40...b6b869 )
by Marco
02:43
created

QueryComponentBoostTrait::buildQueryBoost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 3
crap 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 4
    public function getBoostTerms(): array
29
    {
30 4
        return $this->boostTerms;
31
    }
32
33
    /**
34
     * @param array $boostTerms
35
     * @return QueryComponentFactory
36
     */
37 4
    public function setBoostTerms(array $boostTerms): self
38
    {
39 4
        $this->boostTerms = $boostTerms;
40
41 4
        return $this;
42
    }
43
44
    /**
45
     * Add the index-time boosting to the query
46
     */
47 4
    protected function buildBoosts(): void
48
    {
49 4
        $boosts = $this->query->getBoostedFields();
50 4
        $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 4
        foreach ($boosts as $field => $boost) {
52 1
            foreach ($queries as $term) {
53 1
                $booster = Criteria::where($field)
54 1
                    ->is($term)
55 1
                    ->boost($boost);
56 1
                $this->queryArray[] = $booster->getQuery();
57
            }
58
        }
59 4
    }
60
61
    /**
62
     * Set boosting at Query time
63
     *
64
     * @param array $search
65
     * @param string $term
66
     * @param array $boostTerms
67
     * @return array
68
     */
69 1
    protected function buildQueryBoost($search, string $term, array &$boostTerms): array
70
    {
71 1
        foreach ($search['fields'] as $boostField) {
72 1
            $boostField = str_replace('.', '_', $boostField);
73 1
            $criteria = Criteria::where($boostField)
74 1
                ->is($term)
75 1
                ->boost($search['boost']);
76 1
            $boostTerms[] = $criteria->getQuery();
77
        }
78
79 1
        return $boostTerms;
80
    }
81
}
82