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:
Complex classes like AzureBlobStorage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AzureBlobStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class AzureBlobStorage implements Adapter, |
||
|
|
|||
| 24 | MetadataSupporter |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Error constants. |
||
| 28 | */ |
||
| 29 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
| 30 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
| 34 | */ |
||
| 35 | protected $blobProxyFactory; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $containerName; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $detectContentType; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
| 49 | */ |
||
| 50 | protected $blobProxy; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $multiContainerMode = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var CreateContainerOptions |
||
| 59 | */ |
||
| 60 | protected $createContainerOptions; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
| 64 | * @param string|null $containerName |
||
| 65 | * @param bool $create |
||
| 66 | * @param bool $detectContentType |
||
| 67 | * |
||
| 68 | * @throws \RuntimeException |
||
| 69 | */ |
||
| 70 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
| 71 | { |
||
| 72 | $this->blobProxyFactory = $blobProxyFactory; |
||
| 73 | $this->containerName = $containerName; |
||
| 74 | $this->detectContentType = $detectContentType; |
||
| 75 | if (null === $containerName) { |
||
| 76 | $this->multiContainerMode = true; |
||
| 77 | } elseif ($create) { |
||
| 78 | $this->createContainer($containerName); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return CreateContainerOptions |
||
| 84 | */ |
||
| 85 | public function getCreateContainerOptions() |
||
| 86 | { |
||
| 87 | return $this->createContainerOptions; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param CreateContainerOptions $options |
||
| 92 | */ |
||
| 93 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
| 94 | { |
||
| 95 | $this->createContainerOptions = $options; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates a new container. |
||
| 100 | * |
||
| 101 | * @param string $containerName |
||
| 102 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
| 103 | * |
||
| 104 | * @throws \RuntimeException if cannot create the container |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
| 107 | { |
||
| 108 | $this->init(); |
||
| 109 | |||
| 110 | if (null === $options) { |
||
| 111 | $options = $this->getCreateContainerOptions(); |
||
| 112 | } |
||
| 113 | |||
| 114 | try { |
||
| 115 | $this->blobProxy->createContainer($containerName, $options); |
||
| 116 | } catch (ServiceException $e) { |
||
| 117 | $errorCode = $this->getErrorCodeFromServiceException($e); |
||
| 118 | |||
| 119 | if ($errorCode !== self::ERROR_CONTAINER_ALREADY_EXISTS) { |
||
| 120 | throw new \RuntimeException(sprintf( |
||
| 121 | 'Failed to create the configured container "%s": %s (%s).', |
||
| 122 | $containerName, |
||
| 123 | $e->getErrorText(), |
||
| 124 | $errorCode |
||
| 125 | )); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Deletes a container. |
||
| 132 | * |
||
| 133 | * @param string $containerName |
||
| 134 | * @param DeleteContainerOptions $options |
||
| 135 | * |
||
| 136 | * @throws \RuntimeException if cannot delete the container |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
| 139 | { |
||
| 140 | $this->init(); |
||
| 141 | |||
| 142 | try { |
||
| 143 | $this->blobProxy->deleteContainer($containerName, $options); |
||
| 144 | } catch (ServiceException $e) { |
||
| 145 | $errorCode = $this->getErrorCodeFromServiceException($e); |
||
| 146 | |||
| 147 | if ($errorCode !== self::ERROR_CONTAINER_NOT_FOUND) { |
||
| 148 | throw new \RuntimeException(sprintf( |
||
| 149 | 'Failed to delete the configured container "%s": %s (%s).', |
||
| 150 | $containerName, |
||
| 151 | $e->getErrorText(), |
||
| 152 | $errorCode |
||
| 153 | ), $e->getCode()); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | * @throws \RuntimeException |
||
| 161 | * @throws \InvalidArgumentException |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function read($key) |
|
| 164 | { |
||
| 165 | $this->init(); |
||
| 166 | list($containerName, $key) = $this->tokenizeKey($key); |
||
| 167 | |||
| 168 | try { |
||
| 169 | $blob = $this->blobProxy->getBlob($containerName, $key); |
||
| 170 | |||
| 171 | return stream_get_contents($blob->getContentStream()); |
||
| 172 | } catch (ServiceException $e) { |
||
| 173 | $this->failIfContainerNotFound($e, sprintf('read key "%s"', $key), $containerName); |
||
| 174 | |||
| 175 | return false; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | * @throws \RuntimeException |
||
| 182 | * @throws \InvalidArgumentException |
||
| 183 | */ |
||
| 184 | public function write($key, $content) |
||
| 185 | { |
||
| 186 | $this->init(); |
||
| 187 | list($containerName, $key) = $this->tokenizeKey($key); |
||
| 188 | |||
| 189 | $options = new CreateBlobOptions(); |
||
| 190 | |||
| 191 | if ($this->detectContentType) { |
||
| 192 | $contentType = $this->guessContentType($content); |
||
| 193 | |||
| 194 | $options->setContentType($contentType); |
||
| 195 | } |
||
| 196 | |||
| 197 | try { |
||
| 198 | if ($this->multiContainerMode) { |
||
| 199 | $this->createContainer($containerName); |
||
| 200 | } |
||
| 201 | |||
| 202 | $this->blobProxy->createBlockBlob($containerName, $key, $content, $options); |
||
| 203 | } catch (ServiceException $e) { |
||
| 204 | $this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key), $containerName); |
||
| 205 | |||
| 206 | return false; |
||
| 207 | } |
||
| 208 | if (is_resource($content)) { |
||
| 209 | return Util\Size::fromResource($content); |
||
| 210 | } |
||
| 211 | |||
| 212 | return Util\Size::fromContent($content); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | * @throws \RuntimeException |
||
| 218 | * @throws \InvalidArgumentException |
||
| 219 | */ |
||
| 220 | public function exists($key) |
||
| 221 | { |
||
| 222 | $this->init(); |
||
| 223 | list($containerName, $key) = $this->tokenizeKey($key); |
||
| 224 | |||
| 225 | $listBlobsOptions = new ListBlobsOptions(); |
||
| 226 | $listBlobsOptions->setPrefix($key); |
||
| 227 | |||
| 228 | try { |
||
| 229 | $blobsList = $this->blobProxy->listBlobs($containerName, $listBlobsOptions); |
||
| 230 | |||
| 231 | foreach ($blobsList->getBlobs() as $blob) { |
||
| 232 | if ($key === $blob->getName()) { |
||
| 233 | return true; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } catch (ServiceException $e) { |
||
| 237 | $errorCode = $this->getErrorCodeFromServiceException($e); |
||
| 238 | if ($this->multiContainerMode && self::ERROR_CONTAINER_NOT_FOUND === $errorCode) { |
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | $this->failIfContainerNotFound($e, 'check if key exists', $containerName); |
||
| 242 | |||
| 243 | throw new \RuntimeException(sprintf( |
||
| 244 | 'Failed to check if key "%s" exists in container "%s": %s (%s).', |
||
| 245 | $key, |
||
| 246 | $containerName, |
||
| 247 | $e->getErrorText(), |
||
| 248 | $errorCode |
||
| 249 | ), $e->getCode()); |
||
| 250 | } |
||
| 251 | |||
| 252 | return false; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | * @throws \RuntimeException |
||
| 258 | */ |
||
| 259 | public function keys() |
||
| 260 | { |
||
| 261 | $this->init(); |
||
| 262 | |||
| 263 | try { |
||
| 264 | if ($this->multiContainerMode) { |
||
| 265 | $containersList = $this->blobProxy->listContainers(); |
||
| 266 | return call_user_func_array('array_merge', array_map( |
||
| 267 | function(Container $container) { |
||
| 268 | $containerName = $container->getName(); |
||
| 269 | return $this->fetchBlobs($containerName, $containerName); |
||
| 270 | }, |
||
| 271 | $containersList->getContainers() |
||
| 272 | )); |
||
| 273 | } |
||
| 274 | |||
| 275 | return $this->fetchBlobs($this->containerName); |
||
| 276 | } catch (ServiceException $e) { |
||
| 277 | $this->failIfContainerNotFound($e, 'retrieve keys', $this->containerName); |
||
| 278 | $errorCode = $this->getErrorCodeFromServiceException($e); |
||
| 279 | |||
| 280 | throw new \RuntimeException(sprintf( |
||
| 281 | 'Failed to list keys for the container "%s": %s (%s).', |
||
| 282 | $this->containerName, |
||
| 283 | $e->getErrorText(), |
||
| 284 | $errorCode |
||
| 285 | ), $e->getCode()); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | * @throws \RuntimeException |
||
| 292 | * @throws \InvalidArgumentException |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function mtime($key) |
|
| 295 | { |
||
| 296 | $this->init(); |
||
| 297 | list($containerName, $key) = $this->tokenizeKey($key); |
||
| 298 | |||
| 299 | try { |
||
| 300 | $properties = $this->blobProxy->getBlobProperties($containerName, $key); |
||
| 301 | |||
| 302 | return $properties->getProperties()->getLastModified()->getTimestamp(); |
||
| 303 | } catch (ServiceException $e) { |
||
| 304 | $this->failIfContainerNotFound($e, sprintf('read mtime for key "%s"', $key), $containerName); |
||
| 305 | |||
| 306 | return false; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | * @throws \RuntimeException |
||
| 313 | * @throws \InvalidArgumentException |
||
| 314 | */ |
||
| 315 | View Code Duplication | public function delete($key) |
|
| 316 | { |
||
| 317 | $this->init(); |
||
| 318 | list($containerName, $key) = $this->tokenizeKey($key); |
||
| 319 | |||
| 320 | try { |
||
| 321 | $this->blobProxy->deleteBlob($containerName, $key); |
||
| 322 | |||
| 323 | return true; |
||
| 324 | } catch (ServiceException $e) { |
||
| 325 | $this->failIfContainerNotFound($e, sprintf('delete key "%s"', $key), $containerName); |
||
| 326 | |||
| 327 | return false; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | * @throws \RuntimeException |
||
| 334 | * @throws \InvalidArgumentException |
||
| 335 | */ |
||
| 336 | public function rename($sourceKey, $targetKey) |
||
| 337 | { |
||
| 338 | $this->init(); |
||
| 339 | |||
| 340 | list($sourceContainerName, $sourceKey) = $this->tokenizeKey($sourceKey); |
||
| 341 | list($targetContainerName, $targetKey) = $this->tokenizeKey($targetKey); |
||
| 342 | |||
| 343 | try { |
||
| 344 | if ($this->multiContainerMode) { |
||
| 345 | $this->createContainer($targetContainerName); |
||
| 346 | } |
||
| 347 | $this->blobProxy->copyBlob($targetContainerName, $targetKey, $sourceContainerName, $sourceKey); |
||
| 348 | $this->blobProxy->deleteBlob($sourceContainerName, $sourceKey); |
||
| 349 | |||
| 350 | return true; |
||
| 351 | } catch (ServiceException $e) { |
||
| 352 | $this->failIfContainerNotFound($e, sprintf('rename key "%s"', $sourceKey), $sourceContainerName); |
||
| 353 | |||
| 354 | return false; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | public function isDirectory($key) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | * @throws \RuntimeException |
||
| 370 | * @throws \InvalidArgumentException |
||
| 371 | */ |
||
| 372 | View Code Duplication | public function setMetadata($key, $content) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * {@inheritdoc} |
||
| 394 | * @throws \RuntimeException |
||
| 395 | * @throws \InvalidArgumentException |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function getMetadata($key) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Lazy initialization, automatically called when some method is called after construction. |
||
| 421 | */ |
||
| 422 | protected function init() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
| 431 | * |
||
| 432 | * @param ServiceException $exception |
||
| 433 | * @param string $action |
||
| 434 | * @param string $containerName |
||
| 435 | * |
||
| 436 | * @throws \RuntimeException |
||
| 437 | */ |
||
| 438 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Extracts the error code from a service exception. |
||
| 453 | * |
||
| 454 | * @param ServiceException $exception |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
| 459 | { |
||
| 460 | if (method_exists($exception, 'getErrorReason')) { |
||
| 461 | $xml = @simplexml_load_string($exception->getErrorReason()); |
||
| 462 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * @param string|resource $content |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | View Code Duplication | private function guessContentType($content) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $key |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | * @throws \InvalidArgumentException |
||
| 496 | */ |
||
| 497 | private function tokenizeKey($key) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $containerName |
||
| 518 | * @param null $prefix |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | private function fetchBlobs($containerName, $prefix = null) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Error message to be parsed. |
||
| 539 | * |
||
| 540 | * @param ResponseInterface $response The response with a response body. |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | protected static function parseErrorCode(ResponseInterface $response) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Converts a SimpleXML object to an Array recursively |
||
| 565 | * ensuring all sub-elements are arrays as well. |
||
| 566 | * |
||
| 567 | * @param string $sxml The SimpleXML object. |
||
| 568 | * @param array $arr The array into which to store results. |
||
| 569 | * |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | private static function xmlToArray($sxml, array $arr = null) |
||
| 584 | } |
||
| 585 |