Completed
Pull Request — master (#140)
by Vitaly
04:25
created

AmazonS3v3::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 1
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
}