Passed
Push — hans/or-we-start-or-facets ( e52ce3...da374f )
by Simon
05:18
created

buildAndFacetFilterQuery()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
1
<?php
2
/**
3
 * Trait QueryComponentFacetTrait|Firesphere\SolrSearch\Traits\QueryComponentFacetTrait Trait to set Faceting on fields
4
 * for the {@link \Firesphere\SolrSearch\Factories\QueryComponentFactory}
5
 *
6
 * @package Firesphere\SolrSearch\Traits
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrSearch\Traits;
12
13
use Firesphere\SolrSearch\Indexes\BaseIndex;
14
use Firesphere\SolrSearch\Queries\BaseQuery;
15
use Minimalcode\Search\Criteria;
16
use SilverStripe\Core\ClassInfo;
17
use Solarium\Component\Facet\Field;
18
use Solarium\QueryType\Select\Query\Query;
19
20
/**
21
 * Trait QueryComponentFacetTrait deals with the facets.
22
 *
23
 * Faceting for any given query or index.
24
 *
25
 * @package Firesphere\SolrSearch\Traits
26
 */
27
trait QueryComponentFacetTrait
28
{
29
    /**
30
     * @var BaseIndex Index to query
31
     */
32
    protected $index;
33
    /**
34
     * @var BaseQuery Query to use
35
     */
36
    protected $query;
37
    /**
38
     * @var Query Solarium query
39
     */
40
    protected $clientQuery;
41
42
    /**
43
     * Add facets from the index, to make sure Solr returns
44
     * the expected facets and their respective count on the
45
     * correct fields
46
     */
47 9
    protected function buildQueryFacets(): void
48
    {
49 9
        $facets = $this->clientQuery->getFacetSet();
50 9
        $facetSets = array_merge($this->index->getFacetFields(), $this->index->getOrFacetFields());
51
        // Facets should be set from the index configuration
52 9
        foreach ($facetSets as $class => $config) {
53 2
            $shortClass = getShortFieldName($config['BaseClass']);
54 2
            $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
55
            /** @var Field $facet */
56 2
            $facet = $facets->createFacetField('facet-' . $config['Title']);
57 2
            $facet->setField($field);
58
        }
59
        // Count however, comes from the query
60 9
        $facets->setMinCount($this->query->getFacetsMinCount());
61 9
    }
62
63
    /**
64
     * Add AND facet filters based on the current request
65
     */
66 9
    protected function buildAndFacetFilterQuery()
67
    {
68 9
        $filterFacets = $this->query->getFacetFilter();
69
        /** @var null|Criteria $criteria */
70 9
        $criteria = null;
71 9
        foreach ($this->index->getFacetFields() as $class => $config) {
72 2
            if (isset($filterFacets[$config['Title']])) {
73
                // For the API generator, this needs to be old style list();
74 1
                list($filter, $field) = $this->getFieldFacets($filterFacets, $config);
75 2
                $this->createFacetCriteria($criteria, $field, $filter);
76
            }
77
        }
78 9
        if ($criteria) {
79 1
            $this->clientQuery
80 1
                ->createFilterQuery('facets')
81 1
                ->setQuery($criteria->getQuery());
82
        }
83 9
    }
84
85
    /**
86
     * Add OR facet filters based on the current request
87
     */
88 9
    protected function buildOrFacetFilterQuery()
89
    {
90 9
        $filterFacets = $this->query->getOrFacetFilter();
91
        /** @var null|Criteria $criteria */
92 9
        foreach ($this->index->getOrFacetFields() as $class => $config) {
93
            $criteria = null;
94
            if (isset($filterFacets[$config['Title']])) {
95
                // For the API generator, this needs to be old style list();
96
                list($filter, $field) = $this->getFieldFacets($filterFacets, $config);
97
                $this->createFacetCriteria($criteria, $field, $filter);
98
                $this->clientQuery
99
                    ->createFilterQuery('facets-' . ClassInfo::shortName($class))
100
                    ->setQuery($criteria->getQuery());
101
            }
102
        }
103 9
    }
104
105
    /**
106
     * Combine all facets as AND facet filters for the results
107
     *
108
     * @param null|Criteria $criteria
109
     * @param string $field
110
     * @param array $filter
111
     */
112 1
    protected function createFacetCriteria(&$criteria, string $field, array $filter)
113
    {
114
        // If the criteria is empty, create a new one with a value from the filter array
115 1
        if (!$criteria) {
116 1
            $criteria = Criteria::where($field)->is(array_pop($filter));
117
        }
118
        // Add the other items in the filter array, as an AND
119 1
        foreach ($filter as $filterValue) {
120 1
            $criteria->andWhere($field)->is($filterValue);
121
        }
122 1
    }
123
124
    /**
125
     * Get the field and it's respected values to filter on to generate Criteria from
126
     *
127
     * @param array $filterFacets
128
     * @param array $config
129
     * @return array
130
     */
131 1
    protected function getFieldFacets(array $filterFacets, $config): array
132
    {
133 1
        $filter = $filterFacets[$config['Title']];
134 1
        $filter = is_array($filter) ? $filter : [$filter];
135
        // Fields are "short named" for convenience
136 1
        $shortClass = getShortFieldName($config['BaseClass']);
137 1
        $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
138
139 1
        return [$filter, $field];
140
    }
141
}
142