| Conditions | 4 |
| Paths | 5 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 34 | public function handle(): int |
||
| 35 | { |
||
| 36 | $newIndexName = $this->argument('new-index-name'); |
||
| 37 | $oldIndexName = $this->argument('old-index-name'); |
||
| 38 | $aliasName = $this->argument('alias-name'); |
||
| 39 | |||
| 40 | if (!$this->argumentsAreValid( |
||
| 41 | $newIndexName, |
||
| 42 | $oldIndexName, |
||
| 43 | $aliasName |
||
| 44 | )) { |
||
| 45 | return self::FAILURE; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!$this->client->indices()->exists([ |
||
| 49 | 'index' => $newIndexName, |
||
| 50 | ])) { |
||
| 51 | $this->output->writeln( |
||
| 52 | sprintf( |
||
| 53 | '<error>Index %s cannot be linked to alias because doesn\'t exists.</error>', |
||
| 54 | $newIndexName |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | |||
| 58 | return self::FAILURE; |
||
| 59 | } |
||
| 60 | |||
| 61 | try { |
||
| 62 | $this->client->indices()->putAlias([ |
||
| 63 | 'index' => $newIndexName, |
||
| 64 | 'name' => $aliasName, |
||
| 65 | ]); |
||
| 66 | |||
| 67 | $this->client->indices()->deleteAlias([ |
||
| 68 | 'index' => $oldIndexName, |
||
| 69 | 'name' => $aliasName, |
||
| 70 | ]); |
||
| 71 | } catch (Throwable $exception) { |
||
| 72 | $this->output->writeln( |
||
| 73 | sprintf( |
||
| 74 | '<error>Error switching indexes - new index: %s, old index: %s in alias %s, exception message: %s.</error>', |
||
| 75 | $newIndexName, |
||
| 76 | $oldIndexName, |
||
| 77 | $aliasName, |
||
| 78 | $exception->getMessage() |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | |||
| 82 | return self::FAILURE; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->output->writeln( |
||
| 86 | sprintf( |
||
| 87 | '<info>New index %s linked and old index %s removed from alias %s.</info>', |
||
| 88 | $newIndexName, |
||
| 89 | $oldIndexName, |
||
| 90 | $aliasName |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | |||
| 94 | return self::SUCCESS; |
||
| 95 | } |
||
| 96 | |||
| 135 |