Passed
Pull Request — master (#2925)
by Rafael
35:25
created

SuggestQuery   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 25
c 2
b 0
f 0
dl 0
loc 48
ccs 21
cts 25
cp 0.84
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 30 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query;
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\Domain\Search\Query\ParameterBuilder\ReturnFields;
18
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
19
use ApacheSolrForTypo3\Solr\Util;
20
21
/**
22
 * A query specialized to get search suggestions
23
 *
24
 * @author Ingo Renner <[email protected]>
25
 * @copyright (c) 2009-2015 Ingo Renner <[email protected]>
26
 */
27
class SuggestQuery extends Query
28
{
29
    /**
30
     * @var array
31
     */
32
    protected $configuration;
33
34
    /**
35
     * @var string
36
     */
37
    protected $prefix;
38
39
    /**
40
     * SuggestQuery constructor.
41
     *
42
     * @param string $keywords
43
     * @param TypoScriptConfiguration $solrConfiguration
44
     */
45 2
    public function __construct($keywords, $solrConfiguration = null)
46
    {
47 2
        parent::__construct();
48 2
        $keywords = (string)$keywords;
49
50 2
        $solrConfiguration = $solrConfiguration ?? Util::getSolrConfiguration();
51
52 2
        $this->setQuery($keywords);
53 2
        $this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []);
54
55 2
        if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) {
56
            $this->prefix = $keywords;
57
        } else {
58 2
            $matches = [];
59 2
            preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches);
60 2
            $fullKeywords = trim($matches[2]);
61 2
            $partialKeyword = trim($matches[3]);
62
63 2
            $this->setQuery($fullKeywords);
64 2
            $this->prefix = $partialKeyword;
65
        }
66
67 2
        $this->getEDisMax()->setQueryAlternative('*:*');
68 2
        $this->setFields(ReturnFields::fromString($this->configuration['suggestField'])->getValues());
69 2
        $this->addParam('facet', 'on');
70 2
        $this->addParam('facet.prefix', $this->prefix);
71 2
        $this->addParam('facet.field', $this->configuration['suggestField']);
72 2
        $this->addParam('facet.limit', $this->configuration['numberOfSuggestions']);
73 2
        $this->addParam('facet.mincount', 1);
74 2
        $this->addParam('facet.method', 'enum');
75 2
    }
76
}
77