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

AmazonS3v3   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 4.48 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getBackupFiles() 3 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(Target $target, S3Client $client, string $bucket, string $path)
38
    {
39
        $this->client = $client;
40
        $this->bucket = $bucket;
41
        $this->path = ltrim($path, '/');
42
        $this->setUp($target);
43
    }
44
45
    /**
46
     * Get all created backups.
47
     *
48
     * @return \phpbu\App\Backup\File[]
49
     */
50
    public function getBackupFiles(): array
51
    {
52
        $result = $this->client->listObjects([
53
            'Bucket' => $this->bucket,
54
            'Prefix' => $this->path,
55
            'Delimiter' => '/',
56
        ]);
57
58
        if (!$result['Contents'] || !is_array($result['Contents'])) {
59
            return [];
60
        }
61
62
        foreach ($result['Contents'] as $object) {
63
            // skip currently created backup
64
            if ($object['Key'] == $this->path . $this->target->getFilename()) {
65
                continue;
66
            }
67 View Code Duplication
            if (preg_match('#' . $this->fileRegex . '#i', basename($object['Key']))) {
68
                $this->files[] = new \phpbu\App\Backup\File\AmazonS3v3($this->client, $this->bucket, $object);
69
            }
70
        }
71
72
        return $this->files;
73
    }
74
}