Completed
Pull Request — master (#2)
by Joao
05:22
created

AwsS3Driver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 22.43 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 24
loc 107
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getIterator() 0 13 1
A get() 12 12 1
A put() 0 21 3
A remove() 12 12 1
A getDbConnection() 0 4 1

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
/**
3
 * User: jg
4
 * Date: 08/02/17
5
 * Time: 19:55
6
 */
7
8
namespace ByJG\AnyDataset\Store;
9
10
use Aws\S3\S3Client;
11
use ByJG\AnyDataset\Dataset\GenericIterator;
12
use ByJG\AnyDataset\KeyValueInterface;
13
use ByJG\AnyDataset\Dataset\ArrayDataset;
14
use ByJG\Util\Uri;
15
16
class AwsS3Driver implements KeyValueInterface
17
{
18
19
    /**
20
     * @var S3Client
21
     */
22
    protected $s3Client;
23
24
    /**
25
     * @var string
26
     */
27
    protected $bucket;
28
29
    /**
30
     * AwsS3Driver constructor.
31
     *
32
     *  s3://key:secret@region/bucket
33
     *
34
     * @param string $connectionString
35
     */
36
    public function __construct($connectionString)
37
    {
38
        $uri = new Uri($connectionString);
39
40
        $this->s3Client = new S3Client([
41
            'version'     => 'latest',
42
            'region'      => $uri->getHost(),
43
            'credentials' => [
44
                'key'    => $uri->getUsername(),
45
                'secret' => $uri->getPassword(),
46
            ],
47
        ]);
48
49
        $this->bucket = preg_replace('~^/~', '', $uri->getPath());
50
    }
51
52
    /**
53
     * @param array $options
54
     * @return GenericIterator
55
     */
56
    public function getIterator($options = [])
57
    {
58
        $data = array_merge(
59
            [
60
                'Bucket' => $this->bucket,
61
            ],
62
            $options
63
        );
64
65
        $result = $this->s3Client->listObjects($data);
66
67
        return (new ArrayDataset($result['Contents']))->getIterator();
68
    }
69
70 View Code Duplication
    public function get($key, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $data = array_merge(
73
            [
74
                'Bucket' => $this->bucket,
75
                'Key'    => $key
76
            ],
77
            $options
78
        );
79
80
        return $this->s3Client->getObject($data);
81
    }
82
83
    public function put($key, $value, $contentType = null, $options = [])
84
    {
85
        $data = array_merge(
86
            [
87
                'Bucket' => $this->bucket,
88
                'Key'    => $key,
89
                'Body'   => $value,
90
            ],
91
            $options
92
        );
93
94
        if (!empty($contentType)) {
95
            $data['ContentType'] = $contentType;
96
        }
97
98
        if (!isset($data['ACL'])) {
99
            $data['ACL'] = 'private';
100
        }
101
102
        return $this->s3Client->putObject($data);
103
    }
104
105 View Code Duplication
    public function remove($key, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $data = array_merge(
108
            [
109
                'Bucket' => $this->bucket,
110
                'Key'    => $key
111
            ],
112
            $options
113
        );
114
115
        $this->s3Client->deleteObject($data);
116
    }
117
118
    public function getDbConnection()
119
    {
120
        return $this->s3Client;
121
    }
122
}
123