Passed
Push — master ( d3bd6c...0a0ba7 )
by Simon
01:58
created

FulltextSearchExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 38
c 3
b 0
f 0
dl 0
loc 111
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollatedNice() 0 3 1
A addFulltextFields() 0 6 1
A updateSearchResults() 0 14 1
A initToYml() 0 30 2
A search() 0 13 6
1
<?php
2
/**
3
 * Class FulltextSearchExtension|Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension provide help for
4
 * migrating from the old module
5
 *
6
 * @package Firesphere\SolrCompatibility\Extensions
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrCompatibility\Extensions;
12
13
use Firesphere\SolrSearch\Indexes\BaseIndex;
14
use Firesphere\SolrSearch\Queries\BaseQuery;
15
use Firesphere\SolrSearch\Results\SearchResult;
16
use GuzzleHttp\Exception\GuzzleException;
17
use LogicException;
18
use ReflectionException;
19
use SilverStripe\Control\Controller;
20
use SilverStripe\Core\Extension;
21
use SilverStripe\Dev\Debug;
22
use SilverStripe\ORM\ValidationException;
23
use SilverStripe\View\ArrayData;
24
25
/**
26
 * Class \Firesphere\SolrCompatibility\Extensions\FulltextSearchExtension
27
 * Backward compatibility stubs for the Full text search module
28
 *
29
 * @package Firesphere\SolrCompatibility\Extensions
30
 * @property BaseIndex|FulltextSearchExtension $owner
31
 */
32
class FulltextSearchExtension extends Extension
33
{
34
    /**
35
     * Generate a yml version of the init method indexes
36
     */
37 1
    public function initToYml(): void
38
    {
39
        // @codeCoverageIgnoreStart
40
        if (function_exists('yaml_emit')) {
41
            /** @var BaseIndex $owner */
42
            $owner = $this->owner;
43
            $result = [
44
                BaseIndex::class => [
45
                    $owner->getIndexName() =>
46
                        [
47
                            'Classes'        => $owner->getClasses(),
48
                            'FulltextFields' => array_values($owner->getFulltextFields()),
49
                            'SortFields'     => $owner->getSortFields(),
50
                            'FilterFields'   => $owner->getFilterFields(),
51
                            'BoostedFields'  => $owner->getBoostedFields(),
52
                            'CopyFields'     => $owner->getCopyFields(),
53
                            'DefaultField'   => $owner->getDefaultField(),
54
                            'FacetFields'    => $owner->getFacetFields(),
55
                            'StoredFields'   => $owner->getStoredFields(),
56
                        ],
57
                ],
58
            ];
59
60
            Debug::dump(yaml_emit($result));
61
62
            return;
63
        }
64
        // @codeCoverageIgnoreEnd
65
66 1
        throw new LogicException('yaml-emit PHP module missing');
67
    }
68
69
    /**
70
     * Convert the SearchResult class to a Full text search compatible ArrayData
71
     *
72
     * @param SearchResult|ArrayData $results
73
     */
74 2
    public function updateSearchResults(&$results): void
75
    {
76 2
        $request = Controller::curr()->getRequest();
77
        $data = [
78 2
            'Matches'               => $results->getPaginatedMatches($request),
79 2
            'Facets'                => $results->getFacets(),
80 2
            'Highlights'            => $results->getHighlight(),
81 2
            'Spellcheck'            => $results->getSpellcheck(),
82 2
            'Suggestion'            => $results->getCollatedSpellcheck(),
83 2
            'SuggestionNice'        => $this->getCollatedNice($results->getCollatedSpellcheck()),
84 2
            'SuggestionQueryString' => $results->getCollatedSpellcheck(),
85
        ];
86
        // Override the results with an FTS compatible feature list
87 2
        $results = ArrayData::create($data);
88 2
    }
89
90
    /**
91
     * Create a spellcheck string that's not the literal collation with Solr query parts
92
     *
93
     * @param string $spellcheck
94
     * @return string mixed
95
     */
96 2
    protected function getCollatedNice($spellcheck): string
97
    {
98 2
        return str_replace(' +', ' ', $spellcheck);
99
    }
100
101
    /**
102
     * Convert the old search method to the new BaseIndex doSearch methods
103
     *
104
     * @param BaseQuery $query
105
     * @param int $start deprecated in favour of $query, exists for backward compatibility with FTS
106
     * @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS
107
     * @param array $params deprecated in favour of $query, exists for backward compatibility with FTS
108
     * @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS
109
     * @return SearchResult|ArrayData|mixed
110
     * @throws ValidationException
111
     * @throws GuzzleException
112
     * @throws ReflectionException
113
     * @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query
114
     */
115 2
    public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null)
116
    {
117 2
        $query->getStart() === $start ?: $query->setStart($start);
118 2
        $query->getRows() === $limit ?: $query->setRows($limit);
119 2
        $query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck);
120 2
        if (isset($params['fq']) && !count($query->getFields())) {
121 1
            $query->addField($params['fq']);
122
        }
123
124
        /** @var BaseIndex $owner */
125 2
        $owner = $this->owner;
126
127 2
        return $owner->doSearch($query);
128
    }
129
130
    /**
131
     * Add a Fulltext Field
132
     *
133
     * @param bool $includeSubclasses Compatibility mode, not actually used
134
     * @throws ReflectionException
135
     * @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore
136
     */
137 1
    public function addFulltextFields($includeSubclasses = true)
0 ignored issues
show
Unused Code introduced by
The parameter $includeSubclasses is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

137
    public function addFulltextFields(/** @scrutinizer ignore-unused */ $includeSubclasses = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        /** @var BaseIndex $owner */
140 1
        $owner = $this->owner;
141
142 1
        $owner->addAllFulltextFields();
143 1
    }
144
}
145