Completed
Pull Request — master (#639)
by Tobias
01:43
created

AsyncAwsS3::computeKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Gaufrette\Adapter;
4
5
use AsyncAws\SimpleS3\SimpleS3Client;
6
use Gaufrette\Adapter;
7
use Gaufrette\Util;
8
9
/**
10
 * Amazon S3 adapter using the AsyncAws.
11
 *
12
 * @author  Michael Dowling <[email protected]>
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class AsyncAwsS3 implements Adapter, MetadataSupporter, ListKeysAware, SizeCalculator, MimeTypeProvider
16
{
17
    /** @var SimpleS3Client */
18
    protected $service;
19
    /** @var string */
20
    protected $bucket;
21
    /** @var array */
22
    protected $options;
23
    /** @var bool */
24
    protected $bucketExists;
25
    /** @var array */
26
    protected $metadata = [];
27
    /** @var bool */
28
    protected $detectContentType;
29
30
    /**
31
     * @param SimpleS3Client $service
32
     * @param string   $bucket
33
     * @param array    $options
34
     * @param bool     $detectContentType
35
     */
36 View Code Duplication
    public function __construct(SimpleS3Client $service, $bucket, array $options = [], $detectContentType = false)
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...
37
    {
38
        if (!class_exists(SimpleS3Client::class)) {
39
            throw new \LogicException('You need to install package "async-aws/simple-s3" to use this adapter');
40
        }
41
        $this->service = $service;
42
        $this->bucket = $bucket;
43
        $this->options = array_replace(
44
            [
45
                'create' => false,
46
                'directory' => '',
47
                'acl' => 'private',
48
            ],
49
            $options
50
        );
51
52
        $this->detectContentType = $detectContentType;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 View Code Duplication
    public function setMetadata($key, $content)
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...
59
    {
60
        // BC with AmazonS3 adapter
61
        if (isset($content['contentType'])) {
62
            $content['ContentType'] = $content['contentType'];
63
            unset($content['contentType']);
64
        }
65
66
        $this->metadata[$key] = $content;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getMetadata($key)
73
    {
74
        return isset($this->metadata[$key]) ? $this->metadata[$key] : [];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 View Code Duplication
    public function read($key)
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...
81
    {
82
        $this->ensureBucketExists();
83
        $options = $this->getOptions($key);
84
85
        try {
86
            // Get remote object
87
            $object = $this->service->getObject($options);
88
            // If there's no metadata array set up for this object, set it up
89
            if (!array_key_exists($key, $this->metadata) || !is_array($this->metadata[$key])) {
90
                $this->metadata[$key] = [];
91
            }
92
            // Make remote ContentType metadata available locally
93
            $this->metadata[$key]['ContentType'] = $object->getContentType();
94
95
            return $object->getBody()->getContentAsString();
96
        } catch (\Exception $e) {
97
            return false;
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function rename($sourceKey, $targetKey)
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...
105
    {
106
        $this->ensureBucketExists();
107
        $options = $this->getOptions(
108
            $targetKey,
109
            ['CopySource' => $this->bucket . '/' . $this->computePath($sourceKey)]
110
        );
111
112
        try {
113
            $this->service->copyObject(array_merge($options, $this->getMetadata($targetKey)));
114
115
            return $this->delete($sourceKey);
116
        } catch (\Exception $e) {
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     * @param string|resource $content
124
     */
125
    public function write($key, $content)
126
    {
127
        $this->ensureBucketExists();
128
        $options = $this->getOptions($key);
129
        unset($options['Bucket'], $options['Key']);
130
131
        /*
132
         * If the ContentType was not already set in the metadata, then we autodetect
133
         * it to prevent everything being served up as binary/octet-stream.
134
         */
135 View Code Duplication
        if (!isset($options['ContentType']) && $this->detectContentType) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
136
            $options['ContentType'] = $this->guessContentType($content);
137
        }
138
139
        try {
140
            $this->service->upload($this->bucket, $this->computePath($key), $content, $options);
141
142
            if (is_resource($content)) {
143
                return (int) Util\Size::fromResource($content);
144
            }
145
146
            return Util\Size::fromContent($content);
0 ignored issues
show
Bug introduced by
It seems like $content defined by parameter $content on line 125 can also be of type resource; however, Gaufrette\Util\Size::fromContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
        } catch (\Exception $e) {
148
            return false;
149
        }
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function exists($key)
156
    {
157
        return $this->service->has($this->bucket, $this->computePath($key));
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 View Code Duplication
    public function mtime($key)
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...
164
    {
165
        try {
166
            $result = $this->service->headObject($this->getOptions($key));
167
168
            return $result->getLastModified()->getTimestamp();
169
        } catch (\Exception $e) {
170
            return false;
171
        }
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 View Code Duplication
    public function size($key)
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...
178
    {
179
        try {
180
            $result = $this->service->headObject($this->getOptions($key));
181
182
            return (int) $result->getContentLength();
183
        } catch (\Exception $e) {
184
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Gaufrette\Adapter\SizeCalculator::size of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
185
        }
186
    }
187
188 View Code Duplication
    public function mimeType($key)
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...
189
    {
190
        try {
191
            $result = $this->service->headObject($this->getOptions($key));
192
193
            return $result->getContentType();
194
        } catch (\Exception $e) {
195
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Gaufrette\Adapter\MimeTypeProvider::mimeType of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
196
        }
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function keys()
203
    {
204
        return $this->listKeys();
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 View Code Duplication
    public function listKeys($prefix = '')
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...
211
    {
212
        $this->ensureBucketExists();
213
214
        $options = ['Bucket' => $this->bucket];
215
        if ((string) $prefix != '') {
216
            $options['Prefix'] = $this->computePath($prefix);
217
        } elseif (!empty($this->options['directory'])) {
218
            $options['Prefix'] = $this->options['directory'];
219
        }
220
221
        $keys = [];
222
        $result = $this->service->listObjectsV2($options);
223
        foreach ($result->getContents() as $file) {
224
            $keys[] = $this->computeKey($file->getKey());
225
        }
226
227
        return $keys;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function delete($key)
234
    {
235
        try {
236
            $this->service->deleteObject($this->getOptions($key));
237
238
            return true;
239
        } catch (\Exception $e) {
240
            return false;
241
        }
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function isDirectory($key)
248
    {
249
        $result = $this->service->listObjectsV2([
250
            'Bucket' => $this->bucket,
251
            'Prefix' => rtrim($this->computePath($key), '/') . '/',
252
            'MaxKeys' => 1,
253
        ]);
254
255
        foreach ($result->getContents(true) as $file) {
256
            return true;
257
        }
258
259
        return false;
260
    }
261
262
    /**
263
     * Ensures the specified bucket exists. If the bucket does not exists
264
     * and the create option is set to true, it will try to create the
265
     * bucket. The bucket is created using the same region as the supplied
266
     * client object.
267
     *
268
     * @throws \RuntimeException if the bucket does not exists or could not be
269
     *                           created
270
     */
271 View Code Duplication
    protected function ensureBucketExists()
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...
272
    {
273
        if ($this->bucketExists) {
274
            return true;
275
        }
276
277
        if ($this->bucketExists = $this->service->bucketExists(['Bucket' => $this->bucket])->isSuccess()) {
278
            return true;
279
        }
280
281
        if (!$this->options['create']) {
282
            throw new \RuntimeException(sprintf(
283
                'The configured bucket "%s" does not exist.',
284
                $this->bucket
285
            ));
286
        }
287
288
        $this->service->createBucket([
289
            'Bucket' => $this->bucket,
290
        ]);
291
        $this->bucketExists = true;
292
293
        return true;
294
    }
295
296 View Code Duplication
    protected function getOptions($key, array $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...
297
    {
298
        $options['ACL'] = $this->options['acl'];
299
        $options['Bucket'] = $this->bucket;
300
        $options['Key'] = $this->computePath($key);
301
302
        /*
303
         * Merge global options for adapter, which are set in the constructor, with metadata.
304
         * Metadata will override global options.
305
         */
306
        $options = array_merge($this->options, $options, $this->getMetadata($key));
307
308
        return $options;
309
    }
310
311
    protected function computePath($key)
312
    {
313
        if (empty($this->options['directory'])) {
314
            return $key;
315
        }
316
317
        return sprintf('%s/%s', $this->options['directory'], $key);
318
    }
319
320
    /**
321
     * Computes the key from the specified path.
322
     *
323
     * @param string $path
324
     *
325
     * return string
326
     */
327
    protected function computeKey($path)
328
    {
329
        return ltrim(substr($path, strlen($this->options['directory'])), '/');
330
    }
331
332
    /**
333
     * @param string|resource $content
334
     *
335
     * @return string
336
     */
337 View Code Duplication
    private function guessContentType($content)
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...
338
    {
339
        $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
340
341
        if (is_resource($content)) {
342
            return $fileInfo->file(stream_get_meta_data($content)['uri']);
343
        }
344
345
        return $fileInfo->buffer($content);
346
    }
347
}
348