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