Passed
Push — task/2976_TYPO3.11_compatibili... ( d7dfd0...4be1cc )
by Rafael
03:40
created

SuggestQuery::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2.0003

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 30
ccs 21
cts 22
cp 0.9545
rs 9.568
cc 2
nc 2
nop 2
crap 2.0003
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 7
    public function __construct($keywords, $solrConfiguration = null)
46
    {
47 7
        parent::__construct();
48 7
        $keywords = (string)$keywords;
49
50 7
        $solrConfiguration = $solrConfiguration ?? Util::getSolrConfiguration();
51
52 7
        $this->setQuery($keywords);
53 7
        $this->configuration = $solrConfiguration->getObjectByPathOrDefault('plugin.tx_solr.suggest.', []);
54
55 7
        if (!empty($this->configuration['treatMultipleTermsAsSingleTerm'])) {
56
            $this->prefix = $keywords;
57
        } else {
58 7
            $matches = [];
59 7
            preg_match('/^(:?(.* |))([^ ]+)$/', $keywords, $matches);
60 7
            $fullKeywords = trim($matches[2]);
61 7
            $partialKeyword = trim($matches[3]);
62
63 7
            $this->setQuery($fullKeywords);
64 7
            $this->prefix = $partialKeyword;
65
        }
66
67 7
        $this->getEDisMax()->setQueryAlternative('*:*');
68 7
        $this->setFields(ReturnFields::fromString(($this->configuration['suggestField'] ?? null))->getValues());
69 7
        $this->addParam('facet', 'on');
70 7
        $this->addParam('facet.prefix', $this->prefix);
71 7
        $this->addParam('facet.field', ($this->configuration['suggestField'] ?? null));
72 7
        $this->addParam('facet.limit', ($this->configuration['numberOfSuggestions'] ?? null));
73 7
        $this->addParam('facet.mincount', 1);
74 7
        $this->addParam('facet.method', 'enum');
75 7
    }
76
}
77