Total Complexity | 65 |
Total Lines | 291 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Search, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Search |
||
22 | { |
||
23 | /** |
||
24 | * @var Client |
||
25 | */ |
||
26 | protected $_client; |
||
27 | |||
28 | protected $_query; |
||
29 | protected $_body; |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $_params = []; |
||
34 | |||
35 | public function __construct(Client $client) |
||
36 | { |
||
37 | $this->_client = $client; |
||
38 | $this->_query = new BoolQuery(); |
||
39 | } |
||
40 | |||
41 | public function setIndex($index): self |
||
45 | } |
||
46 | |||
47 | public function setSource($source): self |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param $string |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function search($string): self |
||
58 | { |
||
59 | if (is_object($string)) { |
||
60 | $this->_query = $string; |
||
61 | return $this; |
||
62 | } |
||
63 | $this->_query->must(new QueryString($string)); |
||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | public function match($keywords, $fields = null): self |
||
68 | { |
||
69 | $f = "*"; |
||
70 | if ($fields !== null && is_string($fields)) { |
||
71 | $f = $fields; |
||
72 | } |
||
73 | $this->_query->must(new Match($keywords, $f)); |
||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | public function phrase($string, $fields = null): self |
||
78 | { |
||
79 | $f = "*"; |
||
80 | if ($fields !== null && is_string($fields)) { |
||
81 | $f = $fields; |
||
82 | } |
||
83 | $this->_query->must(new Match($string, $f)); |
||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | public function limit($limit): self |
||
88 | { |
||
89 | $this->_params['limit'] = $limit; |
||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $name |
||
95 | * @param $exp |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function expression($name, $exp): self |
||
99 | { |
||
100 | if (!isset($this->_params['script_fields'])) { |
||
101 | $this->_params['script_fields'] = new ScriptFields(); |
||
102 | } |
||
103 | $this->_params['script_fields']->add($name, $exp); |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function highlight($fields = [],$settings=[]): self |
||
108 | { |
||
109 | |||
110 | if (count($fields) === 0 && count($settings)===0) { |
||
111 | $this->_params['highlight'] = new \stdClass(); |
||
112 | return $this; |
||
113 | } |
||
114 | $this->_params['highlight'] = []; |
||
115 | if (count($fields) > 0) { |
||
116 | $this->_params['highlight']['fields'] =$fields; |
||
117 | } |
||
118 | if(count($settings)>0) { |
||
119 | foreach($settings as $name=>$value) |
||
120 | { |
||
121 | $this->_params['highlight'][$name] =$value; |
||
122 | } |
||
123 | } |
||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | public function distance($args): self |
||
128 | { |
||
129 | $this->_query->must(new Distance($args)); |
||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | public function filter($attr, $op = '', $values = []): self |
||
134 | { |
||
135 | if (is_object($attr)) { |
||
136 | $this->_query->must($attr); |
||
137 | return $this; |
||
138 | } |
||
139 | if (!is_array($values)) { |
||
140 | $values = [$values]; |
||
141 | } |
||
142 | |||
143 | switch ($op) { |
||
144 | case 'range': |
||
145 | $this->_query->must(new Range($attr, [ |
||
146 | 'gte' => $values[0], |
||
147 | 'lte' => $values[1] |
||
148 | ])); |
||
149 | break; |
||
150 | case 'lt': |
||
151 | case 'lte': |
||
152 | case 'gt': |
||
153 | case 'gte': |
||
154 | $this->_query->must(new Range($attr, [ |
||
155 | $op => $values[0], |
||
156 | ])); |
||
157 | break; |
||
158 | case 'equals': |
||
159 | $this->_query->must(new Equals($attr, $values[0])); |
||
160 | break; |
||
161 | } |
||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | public function orFilter($attr, $op = '', $values = []): self |
||
166 | { |
||
167 | if (is_object($attr)) { |
||
168 | $this->_query->should($attr); |
||
169 | return $this; |
||
170 | } |
||
171 | if (!is_array($values)) { |
||
172 | $values = [$values]; |
||
173 | } |
||
174 | switch ($op) { |
||
175 | case 'range': |
||
176 | $this->_query->should(new Range($attr, [ |
||
177 | 'gte' => $values[0], |
||
178 | 'lte' => $values[1] |
||
179 | ])); |
||
180 | break; |
||
181 | case 'lt': |
||
182 | case 'lte': |
||
183 | case 'gte': |
||
184 | case 'gt': |
||
185 | $this->_query->should(new Range($attr, [ |
||
186 | $op => $values[0], |
||
187 | ])); |
||
188 | break; |
||
189 | case 'equals': |
||
190 | $this->_query->should(new Equals($attr, $values[0])); |
||
191 | break; |
||
192 | } |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | public function notFilter($attr, $op = '', $values = []): self |
||
197 | { |
||
198 | if (is_object($attr)) { |
||
199 | $this->_query->mustNot($attr); |
||
200 | return $this; |
||
201 | } |
||
202 | if (!is_array($values)) { |
||
203 | $values = [$values]; |
||
204 | } |
||
205 | |||
206 | switch ($op) { |
||
207 | case 'range': |
||
208 | $this->_query->mustNot(new Range($attr, [ |
||
209 | 'gte' => $values[0], |
||
210 | 'lte' => $values[1] |
||
211 | ])); |
||
212 | break; |
||
213 | case 'lt': |
||
214 | case 'lte': |
||
215 | case 'gte': |
||
216 | case 'gt': |
||
217 | $this->_query->mustNot(new Range($attr, [ |
||
218 | $op => $values[0], |
||
219 | ])); |
||
220 | break; |
||
221 | case 'equals': |
||
222 | $this->_query->mustNot(new Equals($attr, $values[0])); |
||
223 | break; |
||
224 | } |
||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | public function offset($offset): self |
||
229 | { |
||
230 | $this->_params['offset'] = $offset; |
||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | public function maxMatches($maxmatches): self |
||
235 | { |
||
236 | $this->_params['max_matches'] = $maxmatches; |
||
237 | return $this; |
||
238 | } |
||
239 | |||
240 | public function sort($field, $direction = 'asc', $mode = null): self |
||
241 | { |
||
242 | // reset sorting |
||
243 | if ($field === false) { |
||
244 | $this->_params['sort'] = []; |
||
245 | } |
||
246 | //if 1st arg is array means we have a sorting expression |
||
247 | if (is_array($field)) { |
||
248 | //is 2nd arg is true we full set the sort with the expr, otherwise just add it |
||
249 | if (isset($direction) && $direction === true) { |
||
250 | $this->_params['sort'] = $field; |
||
251 | } else { |
||
252 | $this->_params['sort'] [] = $field; |
||
253 | } |
||
254 | return $this; |
||
255 | } |
||
256 | if (!isset($this->_params['sort'])) { |
||
257 | $this->_params['sort'] = []; |
||
258 | } |
||
259 | if ($mode === null) { |
||
260 | $this->_params['sort'] [] = [$field => $direction]; |
||
261 | } else { |
||
262 | $this->_params['sort'] [] = [$field => ['order' => $direction, 'mode' => $mode]]; |
||
263 | } |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | public function profile(): self |
||
269 | { |
||
270 | $this->_params['profile'] = true; |
||
271 | return $this; |
||
272 | } |
||
273 | |||
274 | public function get() |
||
275 | { |
||
276 | $this->_body = $this->compile(); |
||
277 | $resp = $this->_client->search(['body' => $this->_body], true); |
||
278 | return new ResultSet($resp); |
||
279 | |||
280 | } |
||
281 | |||
282 | public function compile() |
||
296 | } |
||
297 | |||
298 | public function getBody() |
||
301 | } |
||
302 | |||
303 | public function reset() |
||
304 | { |
||
305 | $this->_params = []; |
||
306 | $this->_query = new BoolQuery(); |
||
307 | } |
||
308 | |||
309 | public function getClient() |
||
312 | } |
||
313 | } |
||
314 |