Passed
Pull Request — master (#216)
by Simon
06:52 queued 10s
created

QueryComponentFacetTrait::buildQueryFacets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 2
rs 10
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 9
     * the expected facets and their respective count on the
45
     * correct fields
46 9
     */
47
    protected function buildQueryFacets(): void
48 9
    {
49 2
        $facets = $this->clientQuery->getFacetSet();
50 2
        $facetSets = array_merge($this->index->getFacetFields(), $this->index->getOrFacetFields());
51
        // Facets should be set from the index configuration
52 2
        foreach ($facetSets as $class => $config) {
53 2
            $shortClass = getShortFieldName($config['BaseClass']);
54
            $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
55
            /** @var Field $facet */
56 9
            $facet = $facets->createFacetField('facet-' . $config['Title']);
57 9
            $facet->setField($field);
58
        }
59
        // Count however, comes from the query
60
        $facets->setMinCount($this->query->getFacetsMinCount());
61
    }
62 9
63
    /**
64 9
     * Add AND facet filters based on the current request
65
     */
66 9
    protected function buildAndFacetFilterQuery()
67 9
    {
68 2
        $filterFacets = $this->query->getFacetFilter();
69
        /** @var null|Criteria $criteria */
70 1
        $criteria = null;
71 2
        foreach ($this->index->getFacetFields() as $config) {
72
            if (isset($filterFacets[$config['Title']])) {
73
                // For the API generator, this needs to be old style list();
74 9
                list($filter, $field) = $this->getFieldFacets($filterFacets, $config);
75 1
                $this->createFacetCriteria($criteria, $field, $filter);
76 1
            }
77 1
        }
78
        if ($criteria) {
79 9
            $this->clientQuery
80
                ->createFilterQuery('facets')
81
                ->setQuery($criteria->getQuery());
82
        }
83
    }
84
85
    /**
86
     * Get the field and it's respected values to filter on to generate Criteria from
87
     *
88 1
     * @param array $filterFacets
89
     * @param array $config
90 1
     * @return array
91 1
     */
92
    protected function getFieldFacets(array $filterFacets, $config): array
93 1
    {
94 1
        $filter = $filterFacets[$config['Title']];
95
        $filter = is_array($filter) ? $filter : [$filter];
96 1
        // Fields are "short named" for convenience
97
        $shortClass = getShortFieldName($config['BaseClass']);
98
        $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
99
100
        return [$filter, $field];
101
    }
102
103
    /**
104
     * Combine all facets as AND facet filters for the results
105 1
     *
106
     * @param null|Criteria $criteria
107 1
     * @param string $field
108 1
     * @param array $filter
109
     */
110 1
    protected function createFacetCriteria(&$criteria, string $field, array $filter)
111 1
    {
112
        // If the criteria is empty, create a new one with a value from the filter array
113 1
        if (!$criteria) {
114
            $criteria = Criteria::where($field)->is(array_pop($filter));
115
        }
116
        // Add the other items in the filter array, as an AND
117
        foreach ($filter as $filterValue) {
118
            $criteria->andWhere($field)->is($filterValue);
119
        }
120
    }
121
122
    /**
123
     * Add OR facet filters based on the current request
124
     */
125
    protected function buildOrFacetFilterQuery()
126
    {
127
        $filterFacets = $this->query->getOrFacetFilter();
128
        /** @var null|Criteria $criteria */
129
        foreach ($this->index->getOrFacetFields() as $class => $config) {
130
            $criteria = null;
131
            if (isset($filterFacets[$config['Title']])) {
132
                // For the API generator, this needs to be old style list();
133
                list($filter, $field) = $this->getFieldFacets($filterFacets, $config);
134
                $this->createFacetCriteria($criteria, $field, $filter);
135
                $this->clientQuery
136
                    ->createFilterQuery('facets-' . ClassInfo::shortName($class))
137
                    ->setQuery($criteria->getQuery());
138
            }
139
        }
140
    }
141
}
142