Passed
Push — master ( ee50b4...b0990e )
by Simon
08:23 queued 06:41
created

FulltextSearchExtension::addFulltextFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 1
        if (function_exists('yaml_emit')) {
40
            /** @var BaseIndex $owner */
41
            $owner = $this->owner;
42
            $result = [
43
                BaseIndex::class => [
44
                    $owner->getIndexName() =>
45
                        [
46
                            'Classes'        => $owner->getClasses(),
47
                            'FulltextFields' => array_values($owner->getFulltextFields()),
48
                            'SortFields'     => $owner->getSortFields(),
49
                            'FilterFields'   => $owner->getFilterFields(),
50
                            'BoostedFields'  => $owner->getBoostedFields(),
51
                            'CopyFields'     => $owner->getCopyFields(),
52
                            'DefaultField'   => $owner->getDefaultField(),
53
                            'FacetFields'    => $owner->getFacetFields(),
54
                            'StoredFields'   => $owner->getStoredFields(),
55
                        ],
56
                ],
57
            ];
58
59
            Debug::dump(yaml_emit($result));
60
61
            return;
62
        }
63
64 1
        throw new LogicException('yaml-emit PHP module missing');
65
    }
66
67
    /**
68
     * Convert the SearchResult class to a Full text search compatible ArrayData
69
     *
70
     * @param SearchResult|ArrayData $results
71
     */
72 2
    public function updateSearchResults(&$results): void
73
    {
74 2
        $request = Controller::curr()->getRequest();
75
        $data = [
76 2
            'Matches'               => $results->getPaginatedMatches($request),
77 2
            'Facets'                => $results->getFacets(),
78 2
            'Highlights'            => $results->getHighlight(),
79 2
            'Spellcheck'            => $results->getSpellcheck(),
80 2
            'Suggestion'            => $results->getCollatedSpellcheck(),
81 2
            'SuggestionNice'        => $this->getCollatedNice($results->getCollatedSpellcheck()),
82 2
            'SuggestionQueryString' => $results->getCollatedSpellcheck(),
83
        ];
84
        // Override the results with an FTS compatible feature list
85 2
        $results = ArrayData::create($data);
86 2
    }
87
88
    /**
89
     * Create a spellcheck string that's not the literal collation with Solr query parts
90
     *
91
     * @param string $spellcheck
92
     * @return string mixed
93
     */
94 2
    protected function getCollatedNice($spellcheck): string
95
    {
96 2
        return str_replace(' +', ' ', $spellcheck);
97
    }
98
99
    /**
100
     * Convert the old search method to the new BaseIndex doSearch methods
101
     *
102
     * @param BaseQuery $query
103
     * @param int $start deprecated in favour of $query, exists for backward compatibility with FTS
104
     * @param int $limit deprecated in favour of $query, exists for backward compatibility with FTS
105
     * @param array $params deprecated in favour of $query, exists for backward compatibility with FTS
106
     * @param bool $spellcheck deprecated in favour of #query, exists for backward compatibility with FTS
107
     * @return SearchResult|ArrayData|mixed
108
     * @throws ValidationException
109
     * @throws GuzzleException
110
     * @throws ReflectionException
111
     * @deprecated This is used as an Fulltext Search compatibility method. Call doSearch instead with the correct Query
112
     */
113 2
    public function search($query, $start = 0, $limit = 10, $params = [], $spellcheck = null)
114
    {
115 2
        $query->getStart() === $start ?: $query->setStart($start);
116 2
        $query->getRows() === $limit ?: $query->setRows($limit);
117 2
        $query->hasSpellcheck() !== $spellcheck ?: $query->setSpellcheck($spellcheck);
118 2
        if (isset($params['fq']) && !count($query->getFields())) {
119 1
            $query->addField($params['fq']);
120
        }
121
122
        /** @var BaseIndex $owner */
123 2
        $owner = $this->owner;
124
125 2
        return $owner->doSearch($query);
126
    }
127
128
    /**
129
     * Add a Fulltext Field
130
     *
131
     * @param bool $includeSubclasses Compatibility mode, not actually used
132
     * @throws ReflectionException
133
     * @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore
134
     */
135
    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

135
    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...
136
    {
137
        $this->owner->addAllFulltextFields();
0 ignored issues
show
Bug introduced by
The method addAllFulltextFields() does not exist on Firesphere\SolrCompatibi...FulltextSearchExtension. ( Ignorable by Annotation )

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

137
        $this->owner->/** @scrutinizer ignore-call */ 
138
                      addAllFulltextFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
    }
139
}
140