Passed
Push — master ( ba1efb...f2c93c )
by Marco
06:01
created

QueryComponentFacetTrait::buildAndFacetQuery()   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 Solarium\Component\Facet\Field;
17
use Solarium\QueryType\Select\Query\Query;
18
19
/**
20
 * Trait QueryComponentFacetTrait deals with the facets.
21
 *
22
 * Faceting for any given query or index.
23
 *
24
 * @package Firesphere\SolrSearch\Traits
25
 */
26
trait QueryComponentFacetTrait
27
{
28
    /**
29
     * @var BaseIndex Index to query
30
     */
31
    protected $index;
32
    /**
33
     * @var BaseQuery Query to use
34
     */
35
    protected $query;
36
    /**
37
     * @var Query Solarium query
38
     */
39
    protected $clientQuery;
40
41
    /**
42
     * Add facets from the index
43
     */
44 9
    protected function buildFacets(): void
45
    {
46 9
        $facets = $this->clientQuery->getFacetSet();
47
        // Facets should be set from the index configuration
48 9
        foreach ($this->index->getFacetFields() as $class => $config) {
49 2
            $shortClass = getShortFieldName($config['BaseClass']);
50 2
            $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
51
            /** @var Field $facet */
52 2
            $facet = $facets->createFacetField('facet-' . $config['Title']);
53 2
            $facet->setField($field);
54
        }
55
        // Count however, comes from the query
56 9
        $facets->setMinCount($this->query->getFacetsMinCount());
57 9
    }
58
59
    /**
60
     * Add AND facet filters based on the current request
61
     */
62 9
    protected function buildAndFacetQuery()
63
    {
64 9
        $filterFacets = $this->query->getFacetFilter();
65
        /** @var null|Criteria $criteria */
66 9
        $criteria = null;
67 9
        foreach ($this->index->getFacetFields() as $class => $config) {
68 2
            if (isset($filterFacets[$config['Title']])) {
69
                // For the API generator, this needs to be old style list();
70 1
                list($filter, $field) = $this->getFieldFacets($filterFacets, $config);
71 2
                $this->createFacetCriteria($criteria, $field, $filter);
72
            }
73
        }
74 9
        if ($criteria) {
75 1
            $this->clientQuery
76 1
                ->createFilterQuery('facets')
77 1
                ->setQuery($criteria->getQuery());
78
        }
79 9
    }
80
81
    /**
82
     * Combine all facets as AND facet filters for the results
83
     *
84
     * @param null|Criteria $criteria
85
     * @param string $field
86
     * @param array $filter
87
     */
88 1
    protected function createFacetCriteria(&$criteria, string $field, array $filter)
89
    {
90 1
        if (!$criteria) {
91 1
            $criteria = Criteria::where($field)->is(array_pop($filter));
92
        }
93 1
        foreach ($filter as $filterValue) {
94 1
            $criteria->andWhere($field)->is($filterValue);
95
        }
96 1
    }
97
98
    /**
99
     * Get the field and it's respected values to filter on to generate Criteria from
100
     *
101
     * @param array $filterFacets
102
     * @param array $config
103
     * @return array
104
     */
105 1
    protected function getFieldFacets(array $filterFacets, $config): array
106
    {
107 1
        $filter = $filterFacets[$config['Title']];
108 1
        $filter = is_array($filter) ? $filter : [$filter];
109
        // Fields are "short named" for convenience
110 1
        $shortClass = getShortFieldName($config['BaseClass']);
111 1
        $field = $shortClass . '_' . str_replace('.', '_', $config['Field']);
112
113 1
        return [$filter, $field];
114
    }
115
}
116