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 |
||
22 | class AzureBlobStorage implements Adapter, |
||
|
|||
23 | MetadataSupporter |
||
24 | { |
||
25 | /** |
||
26 | * Error constants. |
||
27 | */ |
||
28 | const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
||
29 | const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
||
30 | |||
31 | /** |
||
32 | * @var AzureBlobStorage\BlobProxyFactoryInterface |
||
33 | */ |
||
34 | protected $blobProxyFactory; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $containerName; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $detectContentType; |
||
45 | |||
46 | /** |
||
47 | * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
||
48 | */ |
||
49 | protected $blobProxy; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $multiContainerMode = false; |
||
55 | |||
56 | /** |
||
57 | * @var CreateContainerOptions |
||
58 | */ |
||
59 | protected $createContainerOptions; |
||
60 | |||
61 | /** |
||
62 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
||
63 | * @param string|null $containerName |
||
64 | * @param bool $create |
||
65 | * @param bool $detectContentType |
||
66 | * |
||
67 | * @throws \RuntimeException |
||
68 | */ |
||
69 | public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
||
80 | |||
81 | /** |
||
82 | * @return CreateContainerOptions |
||
83 | */ |
||
84 | public function getCreateContainerOptions() |
||
88 | |||
89 | /** |
||
90 | * @param CreateContainerOptions $options |
||
91 | */ |
||
92 | public function setCreateContainerOptions(CreateContainerOptions $options) |
||
96 | |||
97 | /** |
||
98 | * Creates a new container. |
||
99 | * |
||
100 | * @param string $containerName |
||
101 | * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
||
102 | * |
||
103 | * @throws \RuntimeException if cannot create the container |
||
104 | */ |
||
105 | View Code Duplication | public function createContainer($containerName, CreateContainerOptions $options = null) |
|
128 | |||
129 | /** |
||
130 | * Deletes a container. |
||
131 | * |
||
132 | * @param string $containerName |
||
133 | * @param DeleteContainerOptions $options |
||
134 | * |
||
135 | * @throws \RuntimeException if cannot delete the container |
||
136 | */ |
||
137 | View Code Duplication | public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | * @throws \RuntimeException |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | View Code Duplication | public function read($key) |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | * @throws \RuntimeException |
||
181 | * @throws \InvalidArgumentException |
||
182 | */ |
||
183 | public function write($key, $content) |
||
184 | { |
||
185 | $this->init(); |
||
186 | list($containerName, $key) = $this->tokenizeKey($key); |
||
187 | |||
188 | $options = new CreateBlobOptions(); |
||
189 | |||
190 | if ($this->detectContentType) { |
||
191 | $contentType = $this->guessContentType($content); |
||
192 | |||
193 | $options->setContentType($contentType); |
||
194 | } |
||
195 | |||
196 | try { |
||
197 | $this->createContainer($containerName); |
||
198 | |||
199 | $this->blobProxy->createBlockBlob($containerName, $key, $content, $options); |
||
200 | } catch (ServiceException $e) { |
||
201 | $this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key), $containerName); |
||
202 | |||
203 | return false; |
||
204 | } |
||
205 | if (is_resource($content)) { |
||
206 | return Util\Size::fromResource($content); |
||
207 | } |
||
208 | |||
209 | return Util\Size::fromContent($content); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | * @throws \RuntimeException |
||
215 | * @throws \InvalidArgumentException |
||
216 | */ |
||
217 | public function exists($key) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | * @throws \RuntimeException |
||
255 | */ |
||
256 | public function keys() |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | * @throws \RuntimeException |
||
289 | * @throws \InvalidArgumentException |
||
290 | */ |
||
291 | View Code Duplication | public function mtime($key) |
|
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | * @throws \RuntimeException |
||
310 | * @throws \InvalidArgumentException |
||
311 | */ |
||
312 | View Code Duplication | public function delete($key) |
|
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | * @throws \RuntimeException |
||
331 | * @throws \InvalidArgumentException |
||
332 | */ |
||
333 | public function rename($sourceKey, $targetKey) |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | public function isDirectory($key) |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | * @throws \RuntimeException |
||
365 | * @throws \InvalidArgumentException |
||
366 | */ |
||
367 | View Code Duplication | public function setMetadata($key, $content) |
|
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | * @throws \RuntimeException |
||
390 | * @throws \InvalidArgumentException |
||
391 | */ |
||
392 | View Code Duplication | public function getMetadata($key) |
|
413 | |||
414 | /** |
||
415 | * Lazy initialization, automatically called when some method is called after construction. |
||
416 | */ |
||
417 | protected function init() |
||
423 | |||
424 | /** |
||
425 | * Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
||
426 | * |
||
427 | * @param ServiceException $exception |
||
428 | * @param string $action |
||
429 | * @param string $containerName |
||
430 | * |
||
431 | * @throws \RuntimeException |
||
432 | */ |
||
433 | protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
||
445 | |||
446 | /** |
||
447 | * Extracts the error code from a service exception. |
||
448 | * |
||
449 | * @param ServiceException $exception |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | protected function getErrorCodeFromServiceException(ServiceException $exception) |
||
463 | |||
464 | /** |
||
465 | * @param string|resource $content |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | View Code Duplication | private function guessContentType($content) |
|
479 | |||
480 | /** |
||
481 | * @param string $key |
||
482 | * |
||
483 | * @return array |
||
484 | * @throws \InvalidArgumentException |
||
485 | */ |
||
486 | private function tokenizeKey($key) |
||
487 | { |
||
488 | $containerName = $this->containerName; |
||
489 | if (false === $this->multiContainerMode) { |
||
490 | return [$containerName, $key]; |
||
491 | } |
||
492 | |||
493 | if (false === ($index = strpos($key, '/'))) { |
||
494 | throw new \InvalidArgumentException(sprintf( |
||
495 | 'Failed to establish container name from key "%s", container name is required in multi-container mode', |
||
496 | $key |
||
497 | )); |
||
498 | } |
||
499 | $containerName = substr($key, 0, $index); |
||
500 | $key = substr($key, $index + 1); |
||
501 | |||
502 | return [$containerName, $key]; |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * @param string $containerName |
||
507 | * @param null $prefix |
||
508 | * |
||
509 | * @return array |
||
510 | */ |
||
511 | private function fetchBlobs($containerName, $prefix = null) |
||
525 | } |
||
526 |