Completed
Pull Request — master (#145)
by Vitaly
03:53
created

AmazonS3v3::getPrefix()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
crap 3
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\File\AmazonS3v3 as AwsFile;
7
use phpbu\App\Backup\Path;
8
use phpbu\App\Backup\Target;
9
use phpbu\App\Util;
10
11
/**
12
 * AmazonS3v3 class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @author     Vitaly Baev <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 5.1.0
22
 */
23
class AmazonS3v3 extends Collector
24
{
25
    use Path;
26
27
    /**
28
     * @var \Aws\S3\S3Client
29
     */
30
    protected $client;
31
32
    /**
33
     * AmazonS3 bucket name
34
     *
35
     * @var string
36
     */
37
    protected $bucket;
38
39
    /**
40
     * OpenStack constructor.
41
     *
42
     * @param \phpbu\App\Backup\Target $target
43
     * @param S3Client                 $client
44
     * @param string                   $bucket
45
     * @param string                   $path
46
     * @param int                      $time
47
     */
48 1
    public function __construct(Target $target, S3Client $client, string $bucket, string $path, int $time)
49
    {
50 1
        $this->client = $client;
51 1
        $this->bucket = $bucket;
52 1
        $this->setPath($path, $time);
53 1
        $this->setUp($target);
54 1
    }
55
56
    /**
57
     * Get all created backups.
58
     *
59
     * @return \phpbu\App\Backup\File[]
60
     */
61 1
    public function getBackupFiles() : array
62
    {
63 1
        $result = $this->client->listObjects([
64 1
            'Bucket'    => $this->bucket,
65 1
            'Prefix'    => $this->getPrefix($this->pathNotChanging),
66
        ]);
67
68 1
        if (!isset($result['Contents']) || !$result['Contents'] || !is_array($result['Contents'])) {
69 1
            return [];
70
        }
71
72 1
        foreach ($result['Contents'] as $object) {
73
            // skip currently created backup
74 1
            if ($object['Key'] == $this->getPrefix() . $this->target->getFilename()) {
75 1
                continue;
76
            }
77 1
            if ($this->isFileMatch($object['Key'], Util\Path::withoutLeadingSlash($this->pathRaw))) {
78 1
                $this->files[] = new AwsFile($this->client, $this->bucket, $object);
79
            }
80
        }
81
82 1
        return $this->files;
83
    }
84
85
    /**
86
     * Return prefix for querying remote files and folders
87
     *
88
     * @param string|null $path
89
     * @return string
90
     */
91 1
    protected function getPrefix($path = null): string
92
    {
93 1
        $path = $path ?: $this->path;
94 1
        $prefix = Util\Path::withoutLeadingSlash($path);
95 1
        $prefix = $prefix ? Util\Path::withTrailingSlash($prefix) : '';
96 1
        return $prefix;
97
    }
98
}
99