Completed
Pull Request — master (#472)
by
unknown
02:29
created

AzureBlobStorage::setCreateContainerOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Gaufrette\Adapter;
4
5
use Exception;
6
use Gaufrette\Adapter;
7
use Gaufrette\Util;
8
use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface;
9
use MicrosoftAzure\Storage\Blob\Models\Blob;
10
use MicrosoftAzure\Storage\Blob\Models\Container;
11
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
12
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
13
use MicrosoftAzure\Storage\Blob\Models\DeleteContainerOptions;
14
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
15
use MicrosoftAzure\Storage\Common\ServiceException;
16
use Psr\Http\Message\ResponseInterface;
17
use SimpleXMLElement;
18
19
/**
20
 * Microsoft Azure Blob Storage adapter.
21
 *
22
 * @author Luciano Mammino <[email protected]>
23
 * @author Paweł Czyżewski <[email protected]>
24
 */
25
class AzureBlobStorage implements Adapter,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
26
                                  MetadataSupporter
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 34 found
Loading history...
27
{
28
    /**
29
     * Error constants.
30
     */
31
    const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists';
32
    const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound';
33
34
    /**
35
     * @var AzureBlobStorage\BlobProxyFactoryInterface
36
     */
37
    protected $blobProxyFactory;
38
39
    /**
40
     * @var string
41
     */
42
    protected $containerName;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $detectContentType;
48
49
    /**
50
     * @var \MicrosoftAzure\Storage\Blob\Internal\IBlob
51
     */
52
    protected $blobProxy;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $multiContainerMode = false;
58
59
    /**
60
     * @var CreateContainerOptions
61
     */
62
    protected $createContainerOptions;
63
64
    /**
65
     * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
66
     * @param string|null                                $containerName
67
     * @param bool                                       $create
68
     * @param bool                                       $detectContentType
69
     *
70
     * @throws \RuntimeException
71
     */
72
    public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true)
73
    {
74
        $this->blobProxyFactory = $blobProxyFactory;
75
        $this->containerName = $containerName;
76
        $this->detectContentType = $detectContentType;
77
        if (null === $containerName) {
78
            $this->multiContainerMode = true;
79
        } elseif ($create) {
80
            $this->createContainer($containerName);
81
        }
82
    }
83
84
    /**
85
     * @return CreateContainerOptions
86
     */
87
    public function getCreateContainerOptions()
88
    {
89
        return $this->createContainerOptions;
90
    }
91
92
    /**
93
     * @param CreateContainerOptions $options
94
     */
95
    public function setCreateContainerOptions(CreateContainerOptions $options)
96
    {
97
        $this->createContainerOptions = $options;
98
    }
99
100
    /**
101
     * Creates a new container.
102
     *
103
     * @param string                                                     $containerName
104
     * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options
105
     *
106
     * @throws \RuntimeException if cannot create the container
107
     */
108 View Code Duplication
    public function createContainer($containerName, CreateContainerOptions $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $this->init();
111
112
        if (null === $options) {
113
            $options = $this->getCreateContainerOptions();
114
        }
115
116
        try {
117
            $this->blobProxy->createContainer($containerName, $options);
118
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
            $errorCode = $this->getErrorCodeFromServiceException($e);
120
121
            if ($errorCode !== self::ERROR_CONTAINER_ALREADY_EXISTS) {
122
                throw new \RuntimeException(sprintf(
123
                    'Failed to create the configured container "%s": %s (%s).',
124
                    $containerName,
125
                    $e->getErrorText(),
126
                    $errorCode
127
                ));
128
            }
129
        }
130
    }
131
132
    /**
133
     * Deletes a container.
134
     *
135
     * @param string                 $containerName
136
     * @param DeleteContainerOptions $options
137
     *
138
     * @throws \RuntimeException if cannot delete the container
139
     */
140 View Code Duplication
    public function deleteContainer($containerName, DeleteContainerOptions $options = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $this->init();
143
144
        try {
145
            $this->blobProxy->deleteContainer($containerName, $options);
146
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
147
            $errorCode = $this->getErrorCodeFromServiceException($e);
148
149
            if ($errorCode !== self::ERROR_CONTAINER_NOT_FOUND) {
150
                throw new \RuntimeException(sprintf(
151
                    'Failed to delete the configured container "%s": %s (%s).',
152
                    $containerName,
153
                    $e->getErrorText(),
154
                    $errorCode
155
                ), $e->getCode());
156
            }
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     * @throws \RuntimeException
163
     * @throws \InvalidArgumentException
164
     */
165 View Code Duplication
    public function read($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $this->init();
168
        list($containerName, $key) = $this->tokenizeKey($key);
169
170
        try {
171
            $blob = $this->blobProxy->getBlob($containerName, $key);
172
173
            return stream_get_contents($blob->getContentStream());
174
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
175
            $this->failIfContainerNotFound($e, sprintf('read key "%s"', $key), $containerName);
176
177
            return false;
178
        }
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     * @throws \RuntimeException
184
     * @throws \InvalidArgumentException
185
     */
186
    public function write($key, $content)
187
    {
188
        $this->init();
189
        list($containerName, $key) = $this->tokenizeKey($key);
190
191
        $options = new CreateBlobOptions();
192
193
        if ($this->detectContentType) {
194
            $contentType = $this->guessContentType($content);
195
196
            $options->setContentType($contentType);
197
        }
198
199
        try {
200
            if ($this->multiContainerMode) {
201
                $this->createContainer($containerName);
202
            }
203
204
            $this->blobProxy->createBlockBlob($containerName, $key, $content, $options);
205
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
206
            $this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key), $containerName);
207
208
            return false;
209
        }
210
        if (is_resource($content)) {
211
            return Util\Size::fromResource($content);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Gaufrette\Util\S...fromResource($content); (string) is incompatible with the return type declared by the interface Gaufrette\Adapter::write of type integer|boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
212
        }
213
214
        return Util\Size::fromContent($content);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     * @throws \RuntimeException
220
     * @throws \InvalidArgumentException
221
     */
222
    public function exists($key)
223
    {
224
        $this->init();
225
        list($containerName, $key) = $this->tokenizeKey($key);
226
227
        $listBlobsOptions = new ListBlobsOptions();
228
        $listBlobsOptions->setPrefix($key);
229
230
        try {
231
            $blobsList = $this->blobProxy->listBlobs($containerName, $listBlobsOptions);
232
233
            foreach ($blobsList->getBlobs() as $blob) {
234
                if ($key === $blob->getName()) {
235
                    return true;
236
                }
237
            }
238
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
239
            $errorCode = $this->getErrorCodeFromServiceException($e);
240
            if ($this->multiContainerMode && self::ERROR_CONTAINER_NOT_FOUND === $errorCode) {
241
                return false;
242
            }
243
            $this->failIfContainerNotFound($e, 'check if key exists', $containerName);
244
245
            throw new \RuntimeException(sprintf(
246
                'Failed to check if key "%s" exists in container "%s": %s (%s).',
247
                $key,
248
                $containerName,
249
                $e->getErrorText(),
250
                $errorCode
251
            ), $e->getCode());
252
        }
253
254
        return false;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     * @throws \RuntimeException
260
     */
261
    public function keys()
262
    {
263
        $this->init();
264
265
        try {
266
            if ($this->multiContainerMode) {
267
                $containersList = $this->blobProxy->listContainers();
268
                return call_user_func_array('array_merge', array_map(
269
                    function(Container $container) {
270
                        $containerName = $container->getName();
271
                        return $this->fetchBlobs($containerName, $containerName);
272
                    },
273
                    $containersList->getContainers()
274
                ));
275
            }
276
277
            return $this->fetchBlobs($this->containerName);
278
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
279
            $this->failIfContainerNotFound($e, 'retrieve keys', $this->containerName);
280
            $errorCode = $this->getErrorCodeFromServiceException($e);
281
282
            throw new \RuntimeException(sprintf(
283
                'Failed to list keys for the container "%s": %s (%s).',
284
                $this->containerName,
285
                $e->getErrorText(),
286
                $errorCode
287
            ), $e->getCode());
288
        }
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     * @throws \RuntimeException
294
     * @throws \InvalidArgumentException
295
     */
296 View Code Duplication
    public function mtime($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
    {
298
        $this->init();
299
        list($containerName, $key) = $this->tokenizeKey($key);
300
301
        try {
302
            $properties = $this->blobProxy->getBlobProperties($containerName, $key);
303
304
            return $properties->getProperties()->getLastModified()->getTimestamp();
305
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
306
            $this->failIfContainerNotFound($e, sprintf('read mtime for key "%s"', $key), $containerName);
307
308
            return false;
309
        }
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     * @throws \RuntimeException
315
     * @throws \InvalidArgumentException
316
     */
317 View Code Duplication
    public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
318
    {
319
        $this->init();
320
        list($containerName, $key) = $this->tokenizeKey($key);
321
322
        try {
323
            $this->blobProxy->deleteBlob($containerName, $key);
324
325
            return true;
326
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
327
            $this->failIfContainerNotFound($e, sprintf('delete key "%s"', $key), $containerName);
328
329
            return false;
330
        }
331
    }
332
333
    /**
334
     * {@inheritdoc}
335
     * @throws \RuntimeException
336
     * @throws \InvalidArgumentException
337
     */
338
    public function rename($sourceKey, $targetKey)
339
    {
340
        $this->init();
341
342
        list($sourceContainerName, $sourceKey) = $this->tokenizeKey($sourceKey);
343
        list($targetContainerName, $targetKey) = $this->tokenizeKey($targetKey);
344
345
        try {
346
            if ($this->multiContainerMode) {
347
                $this->createContainer($targetContainerName);
348
            }
349
            $this->blobProxy->copyBlob($targetContainerName, $targetKey, $sourceContainerName, $sourceKey);
350
            $this->blobProxy->deleteBlob($sourceContainerName, $sourceKey);
351
352
            return true;
353
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
354
            $this->failIfContainerNotFound($e, sprintf('rename key "%s"', $sourceKey), $sourceContainerName);
355
356
            return false;
357
        }
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function isDirectory($key)
364
    {
365
        // Windows Azure Blob Storage does not support directories
366
        return false;
367
    }
368
369
    /**
370
     * {@inheritdoc}
371
     * @throws \RuntimeException
372
     * @throws \InvalidArgumentException
373
     */
374 View Code Duplication
    public function setMetadata($key, $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
    {
376
        $this->init();
377
        list($containerName, $key) = $this->tokenizeKey($key);
378
379
        try {
380
            $this->blobProxy->setBlobMetadata($containerName, $key, $content);
381
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
382
            $errorCode = $this->getErrorCodeFromServiceException($e);
383
384
            throw new \RuntimeException(sprintf(
385
                'Failed to set metadata for blob "%s" in container "%s": %s (%s).',
386
                $key,
387
                $containerName,
388
                $e->getErrorText(),
389
                $errorCode
390
            ), $e->getCode());
391
        }
392
    }
393
394
    /**
395
     * {@inheritdoc}
396
     * @throws \RuntimeException
397
     * @throws \InvalidArgumentException
398
     */
399 View Code Duplication
    public function getMetadata($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
400
    {
401
        $this->init();
402
        list($containerName, $key) = $this->tokenizeKey($key);
403
404
        try {
405
            $properties = $this->blobProxy->getBlobProperties($containerName, $key);
406
407
            return $properties->getMetadata();
408
        } catch (ServiceException $e) {
0 ignored issues
show
Bug introduced by
The class MicrosoftAzure\Storage\Common\ServiceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
409
            $errorCode = $this->getErrorCodeFromServiceException($e);
410
411
            throw new \RuntimeException(sprintf(
412
                'Failed to get metadata for blob "%s" in container "%s": %s (%s).',
413
                $key,
414
                $containerName,
415
                $e->getErrorText(),
416
                $errorCode
417
            ), $e->getCode());
418
        }
419
    }
420
421
    /**
422
     * Lazy initialization, automatically called when some method is called after construction.
423
     */
424
    protected function init()
425
    {
426
        if ($this->blobProxy === null) {
427
            $this->blobProxy = $this->blobProxyFactory->create();
428
        }
429
    }
430
431
    /**
432
     * Throws a runtime exception if a give ServiceException derived from a "container not found" error.
433
     *
434
     * @param ServiceException $exception
435
     * @param string           $action
436
     * @param string           $containerName
437
     *
438
     * @throws \RuntimeException
439
     */
440
    protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName)
441
    {
442
        $errorCode = $this->getErrorCodeFromServiceException($exception);
443
444
        if ($errorCode === self::ERROR_CONTAINER_NOT_FOUND) {
445
            throw new \RuntimeException(sprintf(
446
                'Failed to %s: container "%s" not found.',
447
                $action,
448
                $containerName
449
            ), $exception->getCode());
450
        }
451
    }
452
453
    /**
454
     * Extracts the error code from a service exception.
455
     *
456
     * @param ServiceException $exception
457
     *
458
     * @return string
459
     */
460
    protected function getErrorCodeFromServiceException(ServiceException $exception)
461
    {
462
        if (method_exists($exception, 'getErrorReason')) {
463
            $xml = @simplexml_load_string($exception->getErrorReason());
464
465
            if ($xml && isset($xml->Code)) {
466
                return (string) $xml->Code;
467
            }
468
469
            return $exception->getErrorReason();
470
        } else {
471
            /** @var ResponseInterface $response */
472
            $response = $exception->getResponse();
473
            return static::parseErrorCode($response);
474
        }
475
    }
476
477
    /**
478
     * @param string|resource $content
479
     *
480
     * @return string
481
     */
482 View Code Duplication
    private function guessContentType($content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
483
    {
484
        $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
485
486
        if (is_resource($content)) {
487
            return $fileInfo->file(stream_get_meta_data($content)['uri']);
488
        }
489
490
        return $fileInfo->buffer($content);
491
    }
492
493
    /**
494
     * @param string $key
495
     *
496
     * @return array
497
     * @throws \InvalidArgumentException
498
     */
499
    private function tokenizeKey($key)
500
    {
501
        $containerName = $this->containerName;
502
        if (false === $this->multiContainerMode) {
503
            return [$containerName, $key];
504
        }
505
506
        if (false === ($index = strpos($key, '/'))) {
507
            throw new \InvalidArgumentException(sprintf(
508
                'Failed to establish container name from key "%s", container name is required in multi-container mode',
509
                $key
510
            ));
511
        }
512
        $containerName = substr($key, 0, $index);
513
        $key = substr($key, $index + 1);
514
515
        return [$containerName, $key];
516
    }
517
518
    /**
519
     * @param string $containerName
520
     * @param null   $prefix
521
     *
522
     * @return array
523
     */
524
    private function fetchBlobs($containerName, $prefix = null)
525
    {
526
        $blobList = $this->blobProxy->listBlobs($containerName);
527
        return array_map(
528
            function (Blob $blob) use ($prefix) {
529
                $name = $blob->getName();
530
                if (null !== $prefix) {
531
                    $name = $prefix .'/'. $name;
532
                }
533
                return $name;
534
            },
535
            $blobList->getBlobs()
536
        );
537
    }
538
539
    /**
540
     * Error message to be parsed.
541
     *
542
     * @param ResponseInterface $response The response with a response body.
543
     *
544
     * @return string
545
     */
546
    protected static function parseErrorCode(ResponseInterface $response)
547
    {
548
        $errorCode = $response->getReasonPhrase();
549
550
        //try to parse using xml serializer, if failed, return the whole body
551
        //as the error message.
552
        try {
553
            $sxml = new SimpleXMLElement($response->getBody());
554
555
            if (isset($sxml->Code)) {
556
                $errorCode = (string) $sxml->Code;
557
            }
558
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
559
        }
560
561
        return $errorCode;
562
    }
563
}
564