| Total Complexity | 72 |
| Total Lines | 318 |
| 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 |
||
| 23 | class Search |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Client |
||
| 27 | */ |
||
| 28 | protected $client; |
||
| 29 | |||
| 30 | protected $query; |
||
| 31 | protected $body; |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $params = []; |
||
| 36 | |||
| 37 | public function __construct(Client $client) |
||
| 38 | { |
||
| 39 | $this->client = $client; |
||
| 40 | $this->query = new BoolQuery(); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function setIndex($index): self |
||
| 44 | { |
||
| 45 | $this->params['index'] = $index; |
||
| 46 | return $this; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function setSource($source): self |
||
| 50 | { |
||
| 51 | $this->params['_source'] = $source; |
||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $queryString |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function search($queryString): self |
||
| 60 | { |
||
| 61 | if (is_object($queryString)) { |
||
|
|
|||
| 62 | $this->query = $queryString; |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | $this->query->must(new QueryString($queryString)); |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function match($keywords, $fields = null): self |
||
| 70 | { |
||
| 71 | $f = "*"; |
||
| 72 | if ($fields !== null && is_string($fields)) { |
||
| 73 | $f = $fields; |
||
| 74 | } |
||
| 75 | $this->query->must(new Match($keywords, $f)); |
||
| 76 | return $this; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function phrase($string, $fields = null): self |
||
| 80 | { |
||
| 81 | $f = "*"; |
||
| 82 | if ($fields !== null && is_string($fields)) { |
||
| 83 | $f = $fields; |
||
| 84 | } |
||
| 85 | $this->query->must(new MatchPhrase($string, $f)); |
||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function limit($limit): self |
||
| 90 | { |
||
| 91 | $this->params['limit'] = $limit; |
||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $name |
||
| 97 | * @param string $exp |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function expression($name, $exp): self |
||
| 101 | { |
||
| 102 | if (!isset($this->params['script_fields'])) { |
||
| 103 | $this->params['script_fields'] = new ScriptFields(); |
||
| 104 | } |
||
| 105 | $this->params['script_fields']->add($name, $exp); |
||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function highlight($fields = [], $settings = []): self |
||
| 110 | { |
||
| 111 | |||
| 112 | if (count($fields) === 0 && count($settings)===0) { |
||
| 113 | $this->params['highlight'] = new \stdClass(); |
||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | $this->params['highlight'] = []; |
||
| 117 | if (count($fields) > 0) { |
||
| 118 | $this->params['highlight']['fields'] =$fields; |
||
| 119 | } |
||
| 120 | if (count($settings)>0) { |
||
| 121 | foreach ($settings as $name => $value) { |
||
| 122 | $this->params['highlight'][$name] =$value; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function distance($args): self |
||
| 132 | } |
||
| 133 | |||
| 134 | public function filter($attr, $op = '', $values = []): self |
||
| 135 | { |
||
| 136 | if (is_object($attr)) { |
||
| 137 | $this->query->must($attr); |
||
| 138 | return $this; |
||
| 139 | } |
||
| 140 | if (!is_array($values)) { |
||
| 141 | $values = [$values]; |
||
| 142 | } |
||
| 143 | |||
| 144 | switch ($op) { |
||
| 145 | case 'range': |
||
| 146 | $this->query->must(new Range($attr, [ |
||
| 147 | 'gte' => $values[0], |
||
| 148 | 'lte' => $values[1] |
||
| 149 | ])); |
||
| 150 | break; |
||
| 151 | case 'lt': |
||
| 152 | case 'lte': |
||
| 153 | case 'gt': |
||
| 154 | case 'gte': |
||
| 155 | $this->query->must(new Range($attr, [ |
||
| 156 | $op => $values[0], |
||
| 157 | ])); |
||
| 158 | break; |
||
| 159 | case 'in': |
||
| 160 | $this->query->must(new In($attr, $values)); |
||
| 161 | break; |
||
| 162 | case 'equals': |
||
| 163 | $this->query->must(new Equals($attr, $values[0])); |
||
| 164 | break; |
||
| 165 | } |
||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function orFilter($attr, $op = '', $values = []): self |
||
| 201 | } |
||
| 202 | |||
| 203 | public function notFilter($attr, $op = '', $values = []): self |
||
| 236 | } |
||
| 237 | |||
| 238 | public function offset($offset): self |
||
| 239 | { |
||
| 240 | $this->params['offset'] = $offset; |
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function maxMatches($maxmatches): self |
||
| 248 | } |
||
| 249 | |||
| 250 | public function facet($field, $group = null, $limit = null) : self |
||
| 251 | { |
||
| 252 | // reset facets |
||
| 253 | if ($field === false) { |
||
| 254 | $this->params['aggs'] = []; |
||
| 255 | } |
||
| 256 | if ($group === null) { |
||
| 257 | $group = $field; |
||
| 258 | } |
||
| 259 | $terms = ['field'=>$field]; |
||
| 260 | if ($limit !==null) { |
||
| 261 | $terms['size'] = $limit; |
||
| 262 | } |
||
| 263 | $this->params['aggs'][$group] =['terms' =>$terms]; |
||
| 264 | return $this; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function sort($field, $direction = 'asc', $mode = null): self |
||
| 268 | { |
||
| 269 | // reset sorting |
||
| 270 | if ($field === false) { |
||
| 271 | $this->params['sort'] = []; |
||
| 272 | } |
||
| 273 | //if 1st arg is array means we have a sorting expression |
||
| 274 | if (is_array($field)) { |
||
| 275 | //is 2nd arg is true we full set the sort with the expr, otherwise just add it |
||
| 276 | if (isset($direction) && $direction === true) { |
||
| 277 | $this->params['sort'] = $field; |
||
| 278 | } else { |
||
| 279 | $this->params['sort'] [] = $field; |
||
| 280 | } |
||
| 281 | return $this; |
||
| 282 | } |
||
| 283 | if (!isset($this->params['sort'])) { |
||
| 284 | $this->params['sort'] = []; |
||
| 285 | } |
||
| 286 | if ($mode === null) { |
||
| 287 | $this->params['sort'] [] = [$field => $direction]; |
||
| 288 | } else { |
||
| 289 | $this->params['sort'] [] = [$field => ['order' => $direction, 'mode' => $mode]]; |
||
| 290 | } |
||
| 291 | |||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function profile(): self |
||
| 296 | { |
||
| 297 | $this->params['profile'] = true; |
||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return ResultSet |
||
| 303 | */ |
||
| 304 | public function get() |
||
| 305 | { |
||
| 306 | $this->body = $this->compile(); |
||
| 307 | $resp = $this->client->search(['body' => $this->body], true); |
||
| 308 | return new ResultSet($resp); |
||
| 309 | } |
||
| 310 | |||
| 311 | public function compile() |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getBody() |
||
| 330 | } |
||
| 331 | |||
| 332 | public function reset() |
||
| 333 | { |
||
| 334 | $this->params = []; |
||
| 335 | $this->query = new BoolQuery(); |
||
| 336 | } |
||
| 337 | |||
| 338 | public function getClient() |
||
| 341 | } |
||
| 342 | } |
||
| 343 |