Completed
Push — master ( 281cc9...fac822 )
by Sebastian
13:32 queued 10:28
created

AzureBlob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A collectBackups() 0 18 4
A getPrefix() 0 6 2
1
<?php
2
namespace phpbu\App\Backup\Collector;
3
4
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
5
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
6
use phpbu\App\Backup\Collector;
7
use phpbu\App\Backup\File\AzureBlob as BlobFile;
8
use phpbu\App\Backup\Path;
9
use phpbu\App\Backup\Target;
10
use phpbu\App\Util;
11
12
/**
13
 * AzureBlob class.
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @author     Jonathan Bouzekri <[email protected]>
19
 * @copyright  Sebastian Feldmann <[email protected]>
20
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
21
 * @link       http://phpbu.de/
22
 * @since      Class available since Release 5.2.7
23
 */
24
class AzureBlob extends Remote implements Collector
25
{
26
    /**
27
     * @var \MicrosoftAzure\Storage\Blob\BlobRestProxy
28
     */
29
    protected $client;
30
31
    /**
32
     * Azure Blob Storage Container name
33
     *
34
     * @var string
35
     */
36
    protected $containerName;
37
38
    /**
39
     * Amazon S3 constructor.
40
     *
41
     * @param \phpbu\App\Backup\Target                   $target
42
     * @param \phpbu\App\Backup\Path                     $path
43
     * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client
44
     * @param string                                     $containerName
45
     */
46 3
    public function __construct(Target $target, Path $path, BlobRestProxy $client, string $containerName)
47
    {
48 3
        $this->setUp($target, $path);
49 3
        $this->client = $client;
50 3
        $this->containerName = $containerName;
51 3
    }
52
53
    /**
54
     * Collect all created backups.
55
     */
56 3
    protected function collectBackups()
57
    {
58 3
        $listBlobsOptions = new ListBlobsOptions();
59 3
        $listBlobsOptions->setPrefix($this->getPrefix($this->path->getPathThatIsNotChanging()));
60 3
        $listBlobsOptions->setMaxResults(10);
61
62
        do {
63 3
            $blobList = $this->client->listBlobs($this->containerName, $listBlobsOptions);
64 3
            foreach ($blobList->getBlobs() as $blob) {
65 1
                if ($this->isFileMatch($blob->getName())) {
66 1
                    $file                = new BlobFile($this->client, $this->containerName, $blob);
67 1
                    $index               = $this->getFileIndex($file);
68 1
                    $this->files[$index] = $file;
69
                }
70
            }
71 3
            $listBlobsOptions->setContinuationToken($blobList->getContinuationToken());
72 3
        } while ($blobList->getContinuationToken());
73 3
    }
74
75
    /**
76
     * Return prefix for querying remote files and folders
77
     *
78
     * @param string $path
79
     * @return string
80
     */
81 3
    protected function getPrefix($path): string
82
    {
83 3
        $prefix = Util\Path::withoutLeadingSlash($path);
84 3
        $prefix = $prefix ? Util\Path::withTrailingSlash($prefix) : '';
85 3
        return $prefix;
86
    }
87
}
88