| Conditions | 9 |
| Paths | 184 |
| Total Lines | 113 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 214 | private function createElasticsearchIndexes(): int |
||
| 215 | { |
||
| 216 | $this->info('Creating Elasticsearch media indexes...'); |
||
| 217 | |||
| 218 | $dropExisting = $this->option('drop'); |
||
| 219 | |||
| 220 | // Movies index mapping |
||
| 221 | $moviesMapping = [ |
||
| 222 | 'mappings' => [ |
||
| 223 | 'properties' => [ |
||
| 224 | 'imdbid' => ['type' => 'integer'], |
||
| 225 | 'tmdbid' => ['type' => 'integer'], |
||
| 226 | 'traktid' => ['type' => 'integer'], |
||
| 227 | 'title' => ['type' => 'text', 'analyzer' => 'standard'], |
||
| 228 | 'year' => ['type' => 'keyword'], |
||
| 229 | 'genre' => ['type' => 'text'], |
||
| 230 | 'actors' => ['type' => 'text'], |
||
| 231 | 'director' => ['type' => 'text'], |
||
| 232 | 'rating' => ['type' => 'keyword'], |
||
| 233 | 'plot' => ['type' => 'text'], |
||
| 234 | ], |
||
| 235 | ], |
||
| 236 | 'settings' => [ |
||
| 237 | 'number_of_shards' => 1, |
||
| 238 | 'number_of_replicas' => 0, |
||
| 239 | ], |
||
| 240 | ]; |
||
| 241 | |||
| 242 | // TV shows index mapping |
||
| 243 | $tvshowsMapping = [ |
||
| 244 | 'mappings' => [ |
||
| 245 | 'properties' => [ |
||
| 246 | 'title' => ['type' => 'text', 'analyzer' => 'standard'], |
||
| 247 | 'tvdb' => ['type' => 'integer'], |
||
| 248 | 'trakt' => ['type' => 'integer'], |
||
| 249 | 'tvmaze' => ['type' => 'integer'], |
||
| 250 | 'tvrage' => ['type' => 'integer'], |
||
| 251 | 'imdb' => ['type' => 'integer'], |
||
| 252 | 'tmdb' => ['type' => 'integer'], |
||
| 253 | 'started' => ['type' => 'keyword'], |
||
| 254 | 'type' => ['type' => 'integer'], |
||
| 255 | ], |
||
| 256 | ], |
||
| 257 | 'settings' => [ |
||
| 258 | 'number_of_shards' => 1, |
||
| 259 | 'number_of_replicas' => 0, |
||
| 260 | ], |
||
| 261 | ]; |
||
| 262 | |||
| 263 | try { |
||
| 264 | // Create Elasticsearch client directly |
||
| 265 | $esConfig = config('search.drivers.elasticsearch'); |
||
| 266 | $esClient = \Elasticsearch\ClientBuilder::create() |
||
| 267 | ->setHosts($esConfig['hosts'] ?? [['host' => 'localhost', 'port' => 9200]]) |
||
| 268 | ->build(); |
||
| 269 | |||
| 270 | // Create movies index |
||
| 271 | $moviesIndex = config('search.drivers.elasticsearch.indexes.movies', 'movies'); |
||
| 272 | if ($dropExisting && $esClient->indices()->exists(['index' => $moviesIndex])) { |
||
| 273 | $this->warn("Dropping existing index: {$moviesIndex}"); |
||
| 274 | $esClient->indices()->delete(['index' => $moviesIndex]); |
||
| 275 | } |
||
| 276 | |||
| 277 | if (! $esClient->indices()->exists(['index' => $moviesIndex])) { |
||
| 278 | $this->info("Creating index: {$moviesIndex}"); |
||
| 279 | $esClient->indices()->create([ |
||
| 280 | 'index' => $moviesIndex, |
||
| 281 | 'body' => $moviesMapping, |
||
| 282 | ]); |
||
| 283 | $this->info("Index {$moviesIndex} created successfully."); |
||
| 284 | } else { |
||
| 285 | $this->info("Index {$moviesIndex} already exists. Use --drop to recreate."); |
||
| 286 | } |
||
| 287 | |||
| 288 | // Create tvshows index |
||
| 289 | $tvshowsIndex = config('search.drivers.elasticsearch.indexes.tvshows', 'tvshows'); |
||
| 290 | if ($dropExisting && $esClient->indices()->exists(['index' => $tvshowsIndex])) { |
||
| 291 | $this->warn("Dropping existing index: {$tvshowsIndex}"); |
||
| 292 | $esClient->indices()->delete(['index' => $tvshowsIndex]); |
||
| 293 | } |
||
| 294 | |||
| 295 | if (! $esClient->indices()->exists(['index' => $tvshowsIndex])) { |
||
| 296 | $this->info("Creating index: {$tvshowsIndex}"); |
||
| 297 | $esClient->indices()->create([ |
||
| 298 | 'index' => $tvshowsIndex, |
||
| 299 | 'body' => $tvshowsMapping, |
||
| 300 | ]); |
||
| 301 | $this->info("Index {$tvshowsIndex} created successfully."); |
||
| 302 | } else { |
||
| 303 | $this->info("Index {$tvshowsIndex} already exists. Use --drop to recreate."); |
||
| 304 | } |
||
| 305 | |||
| 306 | $this->info('Elasticsearch media indexes created successfully!'); |
||
| 307 | |||
| 308 | // Optionally populate the indexes |
||
| 309 | if ($this->option('populate')) { |
||
| 310 | $this->info('Populating indexes...'); |
||
| 311 | $this->call('nntmux:populate', [ |
||
| 312 | '--elastic' => true, |
||
| 313 | '--movies' => true, |
||
| 314 | ]); |
||
| 315 | $this->call('nntmux:populate', [ |
||
| 316 | '--elastic' => true, |
||
| 317 | '--tvshows' => true, |
||
| 318 | ]); |
||
| 319 | } |
||
| 320 | |||
| 321 | return Command::SUCCESS; |
||
| 322 | |||
| 323 | } catch (\Throwable $e) { |
||
| 324 | $this->error('Failed to create Elasticsearch indexes: '.$e->getMessage()); |
||
| 325 | |||
| 326 | return Command::FAILURE; |
||
| 327 | } |
||
| 330 |