Completed
Pull Request — master (#140)
by
unknown
03:51
created

AmazonS3v3   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 67
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C getBackupFiles() 0 24 7
1
<?php
2
namespace phpbu\App\Backup\Collector;
3
4
use Aws\S3\S3Client;
5
use phpbu\App\Backup\Collector;
6
use phpbu\App\Backup\Target;
7
8
class AmazonS3v3 extends Collector
9
{
10
    /**
11
     * @var S3Client
12
     */
13
    protected $client;
14
15
    /**
16
     * AmazonS3 bucket name
17
     *
18
     * @var string
19
     */
20
    protected $bucket;
21
22
    /**
23
     * AmazonS3 remote path
24
     *
25
     * @var string
26
     */
27
    protected $path;
28
29
    /**
30
     * OpenStack constructor.
31
     *
32
     * @param \phpbu\App\Backup\Target $target
33
     * @param S3Client                 $client
34
     * @param string                   $bucket
35
     * @param string                   $path
36
     */
37 1
    public function __construct(Target $target, S3Client $client, string $bucket, string $path)
38
    {
39 1
        $this->client = $client;
40 1
        $this->bucket = $bucket;
41 1
        $this->path = ltrim($path, '/');
42 1
        $this->setUp($target);
43 1
    }
44
45
    /**
46
     * Get all created backups.
47
     *
48
     * @return \phpbu\App\Backup\File[]
49
     */
50 1
    public function getBackupFiles(): array
51
    {
52 1
        $result = $this->client->listObjects([
53 1
            'Bucket' => $this->bucket,
54 1
            'Prefix' => $this->path,
55 1
            'Delimiter' => '/',
56
        ]);
57
58 1
        if (!isset($result['Contents']) || !$result['Contents'] || !is_array($result['Contents'])) {
59 1
            return [];
60
        }
61
62 1
        foreach ($result['Contents'] as $object) {
63
            // skip currently created backup
64 1
            if ($object['Key'] == $this->path . $this->target->getFilename()) {
65 1
                continue;
66
            }
67 1
            if ($this->isFilenameMatch(basename($object['Key']))) {
68 1
                $this->files[] = new \phpbu\App\Backup\File\AmazonS3v3($this->client, $this->bucket, $object);
69
            }
70
        }
71
72 1
        return $this->files;
73
    }
74
}