Completed
Push — master ( ae0103...9b98c5 )
by Joao
04:23 queued 56s
created

AwsS3Driver::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 2
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