| Total Complexity | 40 |
| Total Lines | 397 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 4 | Features | 0 |
Complex classes like ElasticSearchSiteSearch 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 ElasticSearchSiteSearch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ElasticSearchSiteSearch |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @param string|array $phrases |
||
| 15 | * @param int $limit |
||
| 16 | * @return mixed |
||
| 17 | */ |
||
| 18 | public function indexSearch($phrases, int $limit) |
||
| 19 | { |
||
| 20 | $keywords = $this->sanitize($phrases); |
||
| 21 | if (Str::length($phrases) === 1) { |
||
|
|
|||
| 22 | try { |
||
| 23 | $search = [ |
||
| 24 | 'scroll' => '30s', |
||
| 25 | 'index' => 'releases', |
||
| 26 | 'body' => [ |
||
| 27 | 'query' => [ |
||
| 28 | 'query_string' => [ |
||
| 29 | 'query' => $keywords, |
||
| 30 | 'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name'], |
||
| 31 | 'analyze_wildcard' => true, |
||
| 32 | 'default_operator' => 'and', |
||
| 33 | ], |
||
| 34 | ], |
||
| 35 | 'size' => $limit, |
||
| 36 | 'sort' => [ |
||
| 37 | 'add_date' => [ |
||
| 38 | 'order' => 'desc', |
||
| 39 | ], |
||
| 40 | 'post_date' => [ |
||
| 41 | 'order' => 'desc', |
||
| 42 | ], |
||
| 43 | ], |
||
| 44 | ], |
||
| 45 | ]; |
||
| 46 | |||
| 47 | $results = \Elasticsearch::search($search); |
||
| 48 | |||
| 49 | $searchResult = []; |
||
| 50 | while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
||
| 51 | foreach ($results['hits']['hits'] as $result) { |
||
| 52 | $searchResult[] = $result['_source']['id']; |
||
| 53 | } |
||
| 54 | |||
| 55 | // When done, get the new scroll_id |
||
| 56 | // You must always refresh your _scroll_id! It can change sometimes |
||
| 57 | $scroll_id = $results['_scroll_id']; |
||
| 58 | |||
| 59 | // Execute a Scroll request and repeat |
||
| 60 | $results = \Elasticsearch::scroll([ |
||
| 61 | 'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
||
| 62 | 'scroll' => '30s', // and the same timeout window |
||
| 63 | ] |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | return $searchResult; |
||
| 68 | } catch (BadRequest400Exception $request400Exception) { |
||
| 69 | return []; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string|array $searchName |
||
| 76 | * @param int $limit |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function indexSearchApi($searchName, int $limit) |
||
| 80 | { |
||
| 81 | $keywords = $this->sanitize($searchName); |
||
| 82 | try { |
||
| 83 | $search = [ |
||
| 84 | 'scroll' => '30s', |
||
| 85 | 'index' => 'releases', |
||
| 86 | 'body' => [ |
||
| 87 | 'query' => [ |
||
| 88 | 'query_string' => [ |
||
| 89 | 'query' => $keywords, |
||
| 90 | 'fields' => ['searchname', 'plainsearchname'], |
||
| 91 | 'analyze_wildcard' => true, |
||
| 92 | 'default_operator' => 'and', |
||
| 93 | ], |
||
| 94 | ], |
||
| 95 | 'size' => $limit, |
||
| 96 | 'sort' => [ |
||
| 97 | 'add_date' => [ |
||
| 98 | 'order' => 'desc', |
||
| 99 | ], |
||
| 100 | 'post_date' => [ |
||
| 101 | 'order' => 'desc', |
||
| 102 | ], |
||
| 103 | ], |
||
| 104 | ], |
||
| 105 | ]; |
||
| 106 | |||
| 107 | $results = \Elasticsearch::search($search); |
||
| 108 | |||
| 109 | $searchResult = []; |
||
| 110 | while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
||
| 111 | foreach ($results['hits']['hits'] as $result) { |
||
| 112 | $searchResult[] = $result['_source']['id']; |
||
| 113 | } |
||
| 114 | |||
| 115 | // When done, get the new scroll_id |
||
| 116 | // You must always refresh your _scroll_id! It can change sometimes |
||
| 117 | $scroll_id = $results['_scroll_id']; |
||
| 118 | |||
| 119 | // Execute a Scroll request and repeat |
||
| 120 | $results = \Elasticsearch::scroll([ |
||
| 121 | 'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
||
| 122 | 'scroll' => '30s', // and the same timeout window |
||
| 123 | ] |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $searchResult; |
||
| 128 | } catch (BadRequest400Exception $request400Exception) { |
||
| 129 | return []; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Search function used in TV, TV API, Movies and Anime searches. |
||
| 135 | * @param string|array $name |
||
| 136 | * @param int $limit |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function indexSearchTMA($name, $limit) |
||
| 140 | { |
||
| 141 | $keywords = $this->sanitize($name); |
||
| 142 | try { |
||
| 143 | $search = [ |
||
| 144 | 'scroll' => '30s', |
||
| 145 | 'index' => 'releases', |
||
| 146 | 'body' => [ |
||
| 147 | 'query' => [ |
||
| 148 | 'query_string' => [ |
||
| 149 | 'query' => $keywords, |
||
| 150 | 'fields' => ['searchname', 'plainsearchname'], |
||
| 151 | 'analyze_wildcard' => true, |
||
| 152 | 'default_operator' => 'and', |
||
| 153 | ], |
||
| 154 | ], |
||
| 155 | 'size' => $limit, |
||
| 156 | 'sort' => [ |
||
| 157 | 'add_date' => [ |
||
| 158 | 'order' =>'desc', |
||
| 159 | ], |
||
| 160 | 'post_date' => [ |
||
| 161 | 'order' => 'desc', |
||
| 162 | ], |
||
| 163 | ], |
||
| 164 | ], |
||
| 165 | ]; |
||
| 166 | |||
| 167 | $results = \Elasticsearch::search($search); |
||
| 168 | |||
| 169 | $searchResult = []; |
||
| 170 | while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
||
| 171 | foreach ($results['hits']['hits'] as $result) { |
||
| 172 | $searchResult[] = $result['_source']['id']; |
||
| 173 | } |
||
| 174 | |||
| 175 | // When done, get the new scroll_id |
||
| 176 | // You must always refresh your _scroll_id! It can change sometimes |
||
| 177 | $scroll_id = $results['_scroll_id']; |
||
| 178 | |||
| 179 | // Execute a Scroll request and repeat |
||
| 180 | $results = \Elasticsearch::scroll([ |
||
| 181 | 'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
||
| 182 | 'scroll' => '30s', // and the same timeout window |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | return $searchResult; |
||
| 188 | } catch (BadRequest400Exception $request400Exception) { |
||
| 189 | return []; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string|array $search |
||
| 195 | * @return array|\Illuminate\Support\Collection |
||
| 196 | */ |
||
| 197 | public function predbIndexSearch($search) |
||
| 198 | { |
||
| 199 | try { |
||
| 200 | $search = [ |
||
| 201 | 'scroll' => '30s', |
||
| 202 | 'index' => 'predb', |
||
| 203 | 'body' => [ |
||
| 204 | 'query' => [ |
||
| 205 | 'query_string' => [ |
||
| 206 | 'query' => $search, |
||
| 207 | 'fields' => ['title'], |
||
| 208 | 'analyze_wildcard' => true, |
||
| 209 | 'default_operator' => 'and', |
||
| 210 | ], |
||
| 211 | ], |
||
| 212 | 'size' => 1000, |
||
| 213 | ], |
||
| 214 | ]; |
||
| 215 | |||
| 216 | $results = \Elasticsearch::search($search); |
||
| 217 | |||
| 218 | $ids = []; |
||
| 219 | while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
||
| 220 | foreach ($results['hits']['hits'] as $result) { |
||
| 221 | $ids[] = $result['_source']['id']; |
||
| 222 | } |
||
| 223 | if (empty($ids)) { |
||
| 224 | return collect(); |
||
| 225 | } |
||
| 226 | // When done, get the new scroll_id |
||
| 227 | // You must always refresh your _scroll_id! It can change sometimes |
||
| 228 | $scroll_id = $results['_scroll_id']; |
||
| 229 | |||
| 230 | // Execute a Scroll request and repeat |
||
| 231 | $results = \Elasticsearch::scroll([ |
||
| 232 | 'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
||
| 233 | 'scroll' => '30s', // and the same timeout window |
||
| 234 | ] |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $ids; |
||
| 239 | } catch (BadRequest400Exception $request400Exception) { |
||
| 240 | return []; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array $parameters |
||
| 246 | */ |
||
| 247 | public function insertRelease(array $parameters): void |
||
| 248 | { |
||
| 249 | $searchNameDotless = str_replace(['.', '-'], ' ', $parameters['searchname']); |
||
| 250 | $data = [ |
||
| 251 | 'body' => [ |
||
| 252 | 'id' => $parameters['id'], |
||
| 253 | 'name' => $parameters['name'], |
||
| 254 | 'searchname' => $parameters['searchname'], |
||
| 255 | 'plainsearchname' => $searchNameDotless, |
||
| 256 | 'fromname' => $parameters['fromname'], |
||
| 257 | 'filename' => $parameters['filename'] ?? '', |
||
| 258 | 'add_date' => now()->format('Y-m-d H:i:s'), |
||
| 259 | 'post_date' => $parameters['postdate'], |
||
| 260 | ], |
||
| 261 | 'index' => 'releases', |
||
| 262 | 'id' => $parameters['id'], |
||
| 263 | ]; |
||
| 264 | |||
| 265 | \Elasticsearch::index($data); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param int $id |
||
| 270 | */ |
||
| 271 | public function updateRelease(int $id) |
||
| 272 | { |
||
| 273 | $new = Release::query() |
||
| 274 | ->where('releases.id', $id) |
||
| 275 | ->leftJoin('release_files as rf', 'releases.id', '=', 'rf.releases_id') |
||
| 276 | ->select(['releases.id', 'releases.name', 'releases.searchname', 'releases.fromname', DB::raw('IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename')]) |
||
| 277 | ->groupBy('releases.id') |
||
| 278 | ->first(); |
||
| 279 | if ($new !== null) { |
||
| 280 | $searchNameDotless = str_replace(['.', '-'], ' ', $new->searchname); |
||
| 281 | $data = [ |
||
| 282 | 'body' => [ |
||
| 283 | 'doc' => [ |
||
| 284 | 'id' => $new->id, |
||
| 285 | 'name' => $new->name, |
||
| 286 | 'searchname' => $new->searchname, |
||
| 287 | 'plainsearchname' => $searchNameDotless, |
||
| 288 | 'fromname' => $new->fromname, |
||
| 289 | 'filename' => $new->filename, |
||
| 290 | ], |
||
| 291 | 'doc_as_upsert' => true, |
||
| 292 | ], |
||
| 293 | |||
| 294 | 'index' => 'releases', |
||
| 295 | 'id' => $new->id, |
||
| 296 | ]; |
||
| 297 | |||
| 298 | \Elasticsearch::update($data); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $searchTerm |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function searchPreDb($searchTerm) |
||
| 307 | { |
||
| 308 | $search = [ |
||
| 309 | 'index' => 'predb', |
||
| 310 | 'body' => [ |
||
| 311 | 'query' => [ |
||
| 312 | 'query_string' => [ |
||
| 313 | 'query' => $searchTerm, |
||
| 314 | 'fields' => ['title', 'filename'], |
||
| 315 | 'analyze_wildcard' => true, |
||
| 316 | 'default_operator' => 'and', |
||
| 317 | ], |
||
| 318 | ], |
||
| 319 | ], |
||
| 320 | ]; |
||
| 321 | |||
| 322 | try { |
||
| 323 | $primaryResults = \Elasticsearch::search($search); |
||
| 324 | |||
| 325 | $results = []; |
||
| 326 | foreach ($primaryResults['hits']['hits'] as $primaryResult) { |
||
| 327 | $results[] = $primaryResult['_source']; |
||
| 328 | } |
||
| 329 | } catch (BadRequest400Exception $badRequest400Exception) { |
||
| 330 | return []; |
||
| 331 | } |
||
| 332 | |||
| 333 | return $results; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param $parameters |
||
| 338 | */ |
||
| 339 | public function insertPreDb($parameters) |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $parameters |
||
| 357 | */ |
||
| 358 | public function updatePreDb($parameters) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array|string $phrases |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | private function sanitize($phrases): string |
||
| 408 | } |
||
| 409 | } |
||
| 410 |