Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 21 | class AliasProcessor |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Sets the randomised root name for an index. |
||
| 25 | * |
||
| 26 | * @param IndexConfig $indexConfig |
||
| 27 | * @param Index $index |
||
| 28 | */ |
||
| 29 | 2 | public function setRootName(IndexConfig $indexConfig, Index $index) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Switches an index to become the new target for an alias. Only applies for |
||
| 41 | * indexes that are set to use aliases. |
||
| 42 | * |
||
| 43 | * $force will delete an index encountered where an alias is expected. |
||
| 44 | * |
||
| 45 | * @param IndexConfig $indexConfig |
||
| 46 | * @param Index $index |
||
| 47 | * @param bool $force |
||
| 48 | * @param bool $delete |
||
| 49 | * |
||
| 50 | * @throws AliasIsIndexException |
||
| 51 | */ |
||
| 52 | 7 | public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false, $delete = true) |
|
| 53 | { |
||
| 54 | 7 | $client = $index->getClient(); |
|
| 55 | |||
| 56 | 7 | $aliasName = $indexConfig->getElasticSearchName(); |
|
| 57 | 7 | $oldIndexName = null; |
|
| 58 | 7 | $newIndexName = $index->getName(); |
|
| 59 | |||
| 60 | try { |
||
| 61 | 7 | $oldIndexName = $this->getAliasedIndex($client, $aliasName); |
|
| 62 | 2 | } catch (AliasIsIndexException $e) { |
|
| 63 | 2 | if (!$force) { |
|
| 64 | 1 | throw $e; |
|
| 65 | } |
||
| 66 | |||
| 67 | 1 | if ($delete) { |
|
| 68 | 1 | $this->deleteIndex($client, $aliasName); |
|
| 69 | } else { |
||
| 70 | $this->closeIndex($client, $aliasName); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | try { |
||
| 75 | 6 | $aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName); |
|
| 76 | 6 | $client->request('_aliases', 'POST', $aliasUpdateRequest); |
|
| 77 | 3 | } catch (ExceptionInterface $e) { |
|
| 78 | $this->cleanupRenameFailure($client, $newIndexName, $e); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Delete the old index after the alias has been switched |
||
| 82 | 3 | if (null !== $oldIndexName) { |
|
| 83 | if ($delete) { |
||
| 84 | $this->deleteIndex($client, $oldIndexName); |
||
| 85 | } else { |
||
| 86 | $this->closeIndex($client, $oldIndexName); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | 3 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Builds an ElasticSearch request to rename or create an alias. |
||
| 93 | * |
||
| 94 | * @param string|null $aliasedIndex |
||
| 95 | * @param string $aliasName |
||
| 96 | * @param string $newIndexName |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | 6 | private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName) |
|
| 101 | { |
||
| 102 | 6 | $aliasUpdateRequest = ['actions' => []]; |
|
| 103 | 6 | if (null !== $aliasedIndex) { |
|
| 104 | // if the alias is set - add an action to remove it |
||
| 105 | $aliasUpdateRequest['actions'][] = [ |
||
| 106 | 'remove' => ['index' => $aliasedIndex, 'alias' => $aliasName], |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | |||
| 110 | // add an action to point the alias to the new index |
||
| 111 | 6 | $aliasUpdateRequest['actions'][] = [ |
|
| 112 | 6 | 'add' => ['index' => $newIndexName, 'alias' => $aliasName], |
|
| 113 | ]; |
||
| 114 | |||
| 115 | 6 | return $aliasUpdateRequest; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Cleans up an index when we encounter a failure to rename the alias. |
||
| 120 | * |
||
| 121 | * @param Client $client |
||
| 122 | * @param string $indexName |
||
| 123 | * @param \Exception $renameAliasException |
||
| 124 | */ |
||
| 125 | private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException) |
||
| 126 | { |
||
| 127 | $additionalError = ''; |
||
| 128 | try { |
||
| 129 | $this->deleteIndex($client, $indexName); |
||
| 130 | } catch (ExceptionInterface $deleteNewIndexException) { |
||
| 131 | $additionalError = sprintf( |
||
| 132 | 'Tried to delete newly built index %s, but also failed: %s', |
||
| 133 | $indexName, |
||
| 134 | $deleteNewIndexException->getMessage() |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | throw new \RuntimeException(sprintf( |
||
| 139 | 'Failed to updated index alias: %s. %s', |
||
| 140 | $renameAliasException->getMessage(), |
||
| 141 | $additionalError ?: sprintf('Newly built index %s was deleted', $indexName) |
||
| 142 | ), 0, $renameAliasException); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Delete an index. |
||
| 147 | * |
||
| 148 | * @param Client $client |
||
| 149 | * @param string $indexName Index name to delete |
||
| 150 | */ |
||
| 151 | 1 | View Code Duplication | private function deleteIndex(Client $client, $indexName) |
| 164 | |||
| 165 | /** |
||
| 166 | * Close an index. |
||
| 167 | * |
||
| 168 | * @param Client $client |
||
| 169 | * @param string $indexName |
||
| 170 | */ |
||
| 171 | View Code Duplication | private function closeIndex(Client $client, $indexName) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the name of a single index that an alias points to or throws |
||
| 191 | * an exception if there is more than one. |
||
| 192 | * |
||
| 193 | * @param Client $client |
||
| 194 | * @param string $aliasName Alias name |
||
| 195 | * |
||
| 196 | * @return string|null |
||
| 197 | * |
||
| 198 | * @throws AliasIsIndexException |
||
| 199 | */ |
||
| 200 | 7 | private function getAliasedIndex(Client $client, $aliasName) |
|
| 230 | } |
||
| 231 |