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 declare (strict_types=1); |
||
| 23 | class Server extends OperatorResource implements |
||
|
|
|||
| 24 | Creatable, |
||
| 25 | Updateable, |
||
| 26 | Deletable, |
||
| 27 | Retrievable, |
||
| 28 | Listable |
||
| 29 | { |
||
| 30 | use HasWaiterTrait; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | public $id; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | public $ipv4; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | public $ipv6; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | public $addresses; |
||
| 43 | |||
| 44 | /** @var \DateTimeImmutable */ |
||
| 45 | public $created; |
||
| 46 | |||
| 47 | /** @var \DateTimeImmutable */ |
||
| 48 | public $updated; |
||
| 49 | |||
| 50 | /** @var Flavor */ |
||
| 51 | public $flavor; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | public $hostId; |
||
| 55 | |||
| 56 | /** @var Image */ |
||
| 57 | public $image; |
||
| 58 | |||
| 59 | /** @var array */ |
||
| 60 | public $links; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | public $metadata; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | public $name; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | public $progress; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | public $status; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | public $tenantId; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | public $userId; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | public $adminPass; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | public $taskState; |
||
| 85 | |||
| 86 | protected $resourceKey = 'server'; |
||
| 87 | protected $resourcesKey = 'servers'; |
||
| 88 | protected $markerKey = 'id'; |
||
| 89 | |||
| 90 | protected $aliases = [ |
||
| 91 | 'block_device_mapping_v2' => 'blockDeviceMapping', |
||
| 92 | 'accessIPv4' => 'ipv4', |
||
| 93 | 'accessIPv6' => 'ipv6', |
||
| 94 | 'tenant_id' => 'tenantId', |
||
| 95 | 'user_id' => 'userId', |
||
| 96 | 'security_groups' => 'securityGroups', |
||
| 97 | 'OS-EXT-STS:task_state' => 'taskState', |
||
| 98 | ]; |
||
| 99 | |||
| 100 | /** |
||
| 101 | 2 | * {@inheritDoc} |
|
| 102 | * |
||
| 103 | 2 | * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer} |
|
| 104 | 2 | */ |
|
| 105 | public function create(array $userOptions): Creatable |
||
| 106 | { |
||
| 107 | $response = $this->execute($this->api->postServer(), $userOptions); |
||
| 108 | return $this->populateFromResponse($response); |
||
| 109 | } |
||
| 110 | 1 | ||
| 111 | /** |
||
| 112 | 1 | * {@inheritDoc} |
|
| 113 | */ |
||
| 114 | 1 | public function update() |
|
| 115 | { |
||
| 116 | $response = $this->execute($this->api->putServer(), $this->getAttrs(['id', 'name', 'ipv4', 'ipv6'])); |
||
| 117 | $this->populateFromResponse($response); |
||
| 118 | } |
||
| 119 | |||
| 120 | 1 | /** |
|
| 121 | * {@inheritDoc} |
||
| 122 | 1 | */ |
|
| 123 | 1 | public function delete() |
|
| 124 | { |
||
| 125 | $this->execute($this->api->deleteServer(), $this->getAttrs(['id'])); |
||
| 126 | } |
||
| 127 | |||
| 128 | 1 | /** |
|
| 129 | * {@inheritDoc} |
||
| 130 | 1 | */ |
|
| 131 | public function retrieve() |
||
| 132 | 1 | { |
|
| 133 | $response = $this->execute($this->api->getServer(), $this->getAttrs(['id'])); |
||
| 134 | $this->populateFromResponse($response); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Changes the root password for a server. |
||
| 139 | * |
||
| 140 | 1 | * @param string $newPassword The new root password |
|
| 141 | */ |
||
| 142 | 1 | public function changePassword(string $newPassword) |
|
| 143 | 1 | { |
|
| 144 | $this->execute($this->api->changeServerPassword(), [ |
||
| 145 | 1 | 'id' => $this->id, |
|
| 146 | 1 | 'password' => $newPassword, |
|
| 147 | ]); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Reboots the server. |
||
| 152 | * |
||
| 153 | 2 | * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
|
| 154 | */ |
||
| 155 | 2 | public function reboot(string $type = Enum::REBOOT_SOFT) |
|
| 156 | 1 | { |
|
| 157 | if (!in_array($type, ['SOFT', 'HARD'])) { |
||
| 158 | throw new \RuntimeException('Reboot type must either be SOFT or HARD'); |
||
| 159 | 1 | } |
|
| 160 | 1 | ||
| 161 | 1 | $this->execute($this->api->rebootServer(), [ |
|
| 162 | 1 | 'id' => $this->id, |
|
| 163 | 1 | 'type' => $type, |
|
| 164 | ]); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Rebuilds the server. |
||
| 169 | * |
||
| 170 | 1 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
|
| 171 | */ |
||
| 172 | 1 | public function rebuild(array $options) |
|
| 173 | 1 | { |
|
| 174 | $options['id'] = $this->id; |
||
| 175 | 1 | $response = $this->execute($this->api->rebuildServer(), $options); |
|
| 176 | 1 | ||
| 177 | $this->populateFromResponse($response); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
||
| 182 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
||
| 183 | * |
||
| 184 | 1 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
|
| 185 | */ |
||
| 186 | 1 | public function resize(string $flavorId) |
|
| 187 | 1 | { |
|
| 188 | 1 | $response = $this->execute($this->api->resizeServer(), [ |
|
| 189 | 1 | 'id' => $this->id, |
|
| 190 | 'flavorId' => $flavorId, |
||
| 191 | 1 | ]); |
|
| 192 | 1 | ||
| 193 | $this->populateFromResponse($response); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | 1 | * Confirms a previous resize operation. |
|
| 198 | */ |
||
| 199 | 1 | public function confirmResize() |
|
| 200 | 1 | { |
|
| 201 | $this->execute($this->api->confirmServerResize(), ['confirmResize' => null, 'id' => $this->id]); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | 1 | * Reverts a previous resize operation. |
|
| 206 | */ |
||
| 207 | 1 | public function revertResize() |
|
| 208 | 1 | { |
|
| 209 | $this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Creates an image for the current server. |
||
| 214 | * |
||
| 215 | 1 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
|
| 216 | */ |
||
| 217 | 1 | public function createImage(array $options) |
|
| 218 | 1 | { |
|
| 219 | 1 | $options['id'] = $this->id; |
|
| 220 | $this->execute($this->api->createServerImage(), $options); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Iterates over all the IP addresses for this server. |
||
| 225 | * |
||
| 226 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
| 227 | * |
||
| 228 | 2 | * @return array An array containing to two keys: "public" and "private" |
|
| 229 | */ |
||
| 230 | 2 | public function listAddresses(array $options = []): array |
|
| 231 | { |
||
| 232 | 2 | $options['id'] = $this->id; |
|
| 233 | 2 | ||
| 234 | 2 | $data = (isset($options['networkLabel'])) ? $this->api->getAddressesByNetwork() : $this->api->getAddresses(); |
|
| 235 | $response = $this->execute($data, $options); |
||
| 236 | return Utils::jsonDecode($response)['addresses']; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Retrieves metadata from the API. |
||
| 241 | * |
||
| 242 | 1 | * @return array |
|
| 243 | */ |
||
| 244 | 1 | public function getMetadata(): array |
|
| 245 | 1 | { |
|
| 246 | $response = $this->execute($this->api->getServerMetadata(), ['id' => $this->id]); |
||
| 247 | return $this->parseMetadata($response); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
| 252 | * will either be replaced or removed. |
||
| 253 | * |
||
| 254 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
| 255 | */ |
||
| 256 | 1 | public function resetMetadata(array $metadata) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
| 264 | * referenced in the user options will be replaced with the user's new values. All other |
||
| 265 | * existing keys will remain unaffected. |
||
| 266 | * |
||
| 267 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 1 | public function mergeMetadata(array $metadata) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Retrieve the value for a specific metadata key. |
||
| 279 | * |
||
| 280 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
| 281 | * |
||
| 282 | * @return mixed |
||
| 283 | */ |
||
| 284 | 1 | View Code Duplication | public function getMetadataItem(string $key) |
| 285 | { |
||
| 286 | 1 | $response = $this->execute($this->api->getServerMetadataKey(), ['id' => $this->id, 'key' => $key]); |
|
| 287 | 1 | $value = $this->parseMetadata($response)[$key]; |
|
| 288 | $this->metadata[$key] = $value; |
||
| 289 | return $value; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Remove a specific metadata key. |
||
| 294 | * |
||
| 295 | 1 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
|
| 296 | */ |
||
| 297 | 1 | View Code Duplication | public function deleteMetadataItem(string $key) |
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Add security group to a server (addSecurityGroup action) |
||
| 309 | * |
||
| 310 | * @param array $options {@see \OpenStack\Compute\v2\Api::addSecurityGroup} |
||
| 311 | */ |
||
| 312 | public function addSecurityGroup(array $options) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Add security group to a server (addSecurityGroup action) |
||
| 320 | * |
||
| 321 | * @param array $options {@see \OpenStack\Compute\v2\Api::removeSecurityGroup} |
||
| 322 | */ |
||
| 323 | public function removeSecurityGroup(array $options) |
||
| 328 | |||
| 329 | public function parseMetadata(ResponseInterface $response): array |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns Generator for SecurityGroups |
||
| 336 | * |
||
| 337 | * @return \Generator |
||
| 338 | */ |
||
| 339 | public function listSecurityGroups() |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns Generator for SecurityGroups |
||
| 347 | * |
||
| 348 | * @return \Generator |
||
| 349 | */ |
||
| 350 | public function listVolumeAttachments() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $volumeId |
||
| 357 | * |
||
| 358 | * @return VolumeAttachment |
||
| 359 | */ |
||
| 360 | public function attachVolume($volumeId) |
||
| 366 | |||
| 367 | public function detachVolume($attachmentId) |
||
| 371 | } |
||
| 372 |