AmazonS3v3::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 2
b 0
f 0
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 Remote implements Collector
24
{
25
    /**
26
     * @var \Aws\S3\S3Client
27
     */
28
    protected $client;
29
30
    /**
31
     * Amazon S3 bucket name
32
     *
33
     * @var string
34
     */
35
    protected $bucket;
36
37
    /**
38
     * Amazon S3 constructor.
39
     *
40
     * @param \phpbu\App\Backup\Target $target
41
     * @param \phpbu\App\Backup\Path   $path
42
     * @param \Aws\S3\S3Client         $client
43
     * @param string                   $bucket
44
     */
45 3
    public function __construct(Target $target, Path $path, S3Client $client, string $bucket)
46
    {
47 3
        $this->setUp($target, $path);
48 3
        $this->client = $client;
49 3
        $this->bucket = $bucket;
50 3
    }
51
52
    /**
53
     * Collect all created backups.
54
     */
55 3
    protected function collectBackups()
56
    {
57 3
        $result = $this->client->listObjects([
58 3
            'Bucket' => $this->bucket,
59 3
            'Prefix' => $this->getPrefix($this->path->getPathThatIsNotChanging()),
60
        ]);
61
62 3
        if (!isset($result['Contents']) || !$result['Contents'] || !is_array($result['Contents'])) {
63 2
            return;
64
        }
65
66 1
        foreach ($result['Contents'] as $object) {
67 1
            if ($this->isFileMatch($object['Key'])) {
68 1
                $file                = new AwsFile($this->client, $this->bucket, $object);
69 1
                $index               = $this->getFileIndex($file);
70 1
                $this->files[$index] = $file;
71
            }
72
        }
73 1
    }
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