Passed
Push — master ( 42d2d3...8f9ec7 )
by Timo
69:32 queued 48:37
created

DefaultFacetQueryBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2.004
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
*/
16
17
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
18
19
class DefaultFacetQueryBuilder implements FacetQueryBuilderInterface {
20
21
    /**
22
     * @param string $facetName
23
     * @param TypoScriptConfiguration $configuration
24
     * @return array
25
     */
26 28
    public function build($facetName, TypoScriptConfiguration $configuration)
27
    {
28 28
        $facetParameters = [];
29 28
        $facetConfiguration = $configuration->getSearchFacetingFacetByName($facetName);
30
31 28
        $tags = $this->buildExcludeTags($facetConfiguration, $configuration);
32 28
        $facetParameters['facet.field'][] = $tags . $facetConfiguration['field'];
33
34 28
        $sortingExpression = new SortingExpression();
35 28
        $facetSortExpression = $sortingExpression->getForFacet($facetConfiguration['sortBy']);
36 28
        if (!empty($facetSortExpression)) {
37
            $facetParameters['f.' . $facetConfiguration['field'] . '.facet.sort'] = $facetSortExpression;
38
        }
39
40 28
        return $facetParameters;
41
    }
42
43
    /**
44
     * @param array $facetConfiguration
45
     * @param TypoScriptConfiguration $configuration
46
     * @return string
47
     */
48 31
    protected function buildExcludeTags(array $facetConfiguration, TypoScriptConfiguration $configuration)
49
    {
50
        // simple for now, may add overrides f.<field_name>.facet.* later
51 31
        if ($configuration->getSearchFacetingKeepAllFacetsOnSelection()) {
52 1
            $facets = [];
53 1
            foreach ($configuration->getSearchFacetingFacets() as $facet) {
54 1
                $facets[] = $facet['field'];
55
            }
56
57 1
            return '{!ex=' . implode(',', $facets) . '}';
58 30
        } elseif ($facetConfiguration['keepAllOptionsOnSelection'] == 1) {
59 1
            return '{!ex=' . $facetConfiguration['field'] . '}';
60
        }
61
62 29
        return '';
63
    }
64
}
65
66