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) |
|
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 | 5 | private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName) |
|
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 | 1 | private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException) |
|
126 | { |
||
127 | 1 | $additionalError = ''; |
|
128 | try { |
||
129 | 1 | $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 | 1 | throw new \RuntimeException(sprintf( |
|
139 | 1 | 'Failed to updated index alias: %s. %s', |
|
140 | 1 | $renameAliasException->getMessage(), |
|
141 | 1 | $additionalError ?: sprintf('Newly built index %s was deleted', $indexName) |
|
142 | 1 | ), 0, $renameAliasException); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Delete an index. |
||
147 | * |
||
148 | * @param Client $client |
||
149 | * @param string $indexName Index name to delete |
||
150 | */ |
||
151 | 4 | View Code Duplication | private function deleteIndex(Client $client, $indexName) |
152 | { |
||
153 | try { |
||
154 | 4 | $path = sprintf('%s', $indexName); |
|
155 | 4 | $client->request($path, Request::DELETE); |
|
156 | } catch (ExceptionInterface $deleteOldIndexException) { |
||
157 | throw new \RuntimeException(sprintf( |
||
158 | 'Failed to delete index %s with message: %s', |
||
159 | $indexName, |
||
160 | $deleteOldIndexException->getMessage() |
||
161 | ), 0, $deleteOldIndexException); |
||
162 | } |
||
163 | 4 | } |
|
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) |
|
172 | { |
||
173 | try { |
||
174 | $path = sprintf('%s/_close', $indexName); |
||
175 | $client->request($path, Request::POST); |
||
176 | } catch (ExceptionInterface $e) { |
||
177 | throw new \RuntimeException( |
||
178 | sprintf( |
||
179 | 'Failed to close index %s with message: %s', |
||
180 | $indexName, |
||
181 | $e->getMessage() |
||
182 | ), |
||
183 | 0, |
||
184 | $e |
||
185 | ); |
||
186 | } |
||
187 | } |
||
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 |