Passed
Push — master ( 3aba61...0fd263 )
by Gordon
05:45 queued 02:34
created

Searcher::search()   F

Complexity

Conditions 15
Paths 324

Size

Total Lines 122
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 15
eloc 71
c 3
b 0
f 0
nc 324
nop 1
dl 0
loc 122
rs 3.5333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 1:35 น.
8
 */
9
10
namespace Suilven\ManticoreSearch\Service;
11
12
use Manticoresearch\Search;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DataObject;
15
use Suilven\FreeTextSearch\Container\SearchResults;
16
use Suilven\FreeTextSearch\Indexes;
17
18
class Searcher extends \Suilven\FreeTextSearch\Base\Searcher implements \Suilven\FreeTextSearch\Interfaces\Searcher
19
{
20
    /** @var \Suilven\ManticoreSearch\Service\Client */
21
    private $client;
22
23
    public function __construct()
24
    {
25
        $this->client = new Client();
26
    }
27
28
29
    public function search(?string $q): SearchResults
30
    {
31
        $startTime = \microtime(true);
32
        $client = new Client();
33
        $manticoreClient = $client->getConnection();
34
35
        $searcher = new Search($manticoreClient);
36
        $searcher->setIndex($this->indexName);
37
38
        $searcher->limit($this->pageSize);
39
        $offset=$this->pageSize * ($this->page-1);
40
        $searcher->offset($offset);
41
42
        $indexes = new Indexes();
43
        $index = $indexes->getIndex($this->indexName);
44
45
        $searcher->highlight(
46
            [],
47
            ['pre_tags' => '<b>', 'post_tags'=>'</b>']
48
        );
49
50
        $q = \is_null($q)
51
            ? ''
52
            : $q;
53
54
        $manticoreResult = $searcher->search($q)->get();
55
56
        $allFields = \array_merge(
57
            $index->getFields(),
58
            $index->getTokens(),
59
            //$index->getHasManyFields(),
60
            $index->getHasOneFields(),
61
            $index->getStoredFields()
62
        );
63
64
65
        $hasManyFields = $index->getHasManyFields();
66
        foreach (\array_keys($hasManyFields) as $key) {
67
            $allFields[] = $key;
68
        }
69
70
        $ssResult = new ArrayList();
71
        while ($manticoreResult->valid()) {
72
            $hit = $manticoreResult->current();
73
            $source = $hit->getData();
74
            //print_r($source);
75
            $ssDataObject = new DataObject();
76
77
            $keys = \array_keys($source);
78
79
            foreach ($keys as $key) {
80
                $keyname = $key;
81
                foreach ($allFields as $field) {
82
                    if (\strtolower($field) === $key) {
83
                        $keyname = $field;
84
85
                        break;
86
                    }
87
                }
88
89
                // @todo This is a hack as $Title is rendering the ID in the template
90
                if ($keyname === 'Title') {
91
                    $keyname = 'ResultTitle';
92
                } elseif ($keyname === 'link') {
93
                    $keyname = 'Link';
94
                };
95
96
                /** @phpstan-ignore-next-line */
97
                $ssDataObject->$keyname = $source[$key];
98
            }
99
100
101
            // manticore lowercases fields, so as above normalize them back to the SS fieldnames
102
            $highlights = $hit->getHighlight();
103
            $highlightsSS = [];
104
105
            $fieldsToHighlight = $index->getHighlightedFields();
106
107
            $keys = \array_keys($highlights);
108
            foreach ($keys as $key) {
109
                if (!isset($highlights[$key]) || !\in_array($key, $fieldsToHighlight, true)) {
110
                    continue;
111
                }
112
                $keyname = $key;
113
                foreach ($allFields as $field) {
114
                    if (\strtolower($field) === $key) {
115
                        $keyname = $field;
116
117
                        continue;
118
                    }
119
                }
120
121
                if ($key === 'link') {
122
                    $keyname = 'Link';
123
                }
124
125
                $highlightsSS[$keyname] = $highlights[$key];
126
            }
127
128
            /** @phpstan-ignore-next-line */
129
            $ssDataObject->Highlights = $highlightsSS;
130
131
            $ssDataObject->ID = $hit->getId();
132
            $ssResult->push($ssDataObject);
133
            $manticoreResult->next();
134
        }
135
136
        // we now need to standardize the output returned
137
138
        $searchResults = new SearchResults();
139
        $searchResults->setRecords($ssResult);
140
        $searchResults->setPage($this->page);
141
        $searchResults->setPageSize($this->pageSize);
142
        $searchResults->setQuery($q);
143
        $searchResults->setTotalNumberOfResults($manticoreResult->getTotal());
144
145
        $endTime = \microtime(true);
146
        $delta = $endTime - $startTime;
147
        $delta = \round(1000*$delta)/1000;
148
        $searchResults->setTime($delta);
149
150
        return $searchResults;
151
    }
152
}
153