Completed
Push — master ( 6a0c0e...a2af9a )
by Albin
05:16
created

AwsS3SetUpTearDownTrait::setUp()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
rs 8.439
cc 6
eloc 19
nc 4
nop 0
1
<?php
2
3
namespace Gaufrette\Extras\Tests\Resolvable\Resolver;
4
5
use Aws\S3\S3Client;
6
7
/**
8
 * @TODO: To be removed
9
 * @see https://github.com/KnpLabs/Gaufrette/pull/494
10
 */
11
trait AwsS3SetUpTearDownTrait
12
{
13
    /** @var string */
14
    private $bucket;
15
16
    /** @var S3Client */
17
    private $client;
18
19
    public function setUp()
20
    {
21
        $key    = getenv('AWS_KEY');
22
        $secret = getenv('AWS_SECRET');
23
        $region = getenv('AWS_REGION');
24
25
        if (empty($key) || empty($secret)) {
26
            $this->markTestSkipped('Missing AWS_KEY and/or AWS_SECRET env vars.');
0 ignored issues
show
Bug introduced by
It seems like markTestSkipped() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
        }
28
29
        $this->bucket = uniqid(getenv('AWS_BUCKET'));
30
31
        // For AWS SDK v2
32
        if (class_exists('Aws\Common\Client\AbstractClient')) {
33
            return $this->client = S3Client::factory([
0 ignored issues
show
Deprecated Code introduced by
The method Aws\AwsClient::factory() has been deprecated.

This method has been deprecated.

Loading history...
34
                'region' => $region ? $region : 'eu-west-1',
35
                'version' => '2006-03-01',
36
                'key' => $key,
37
                'secret' => $secret,
38
            ]);
39
        }
40
41
        // For AWS SDK v3
42
        $this->client = new S3Client([
43
            'region'      => $region ? $region : 'eu-west-1',
44
            'version'     => 'latest',
45
            'credentials' => [
46
                'key'    => $key,
47
                'secret' => $secret,
48
            ],
49
        ]);
50
    }
51
52
    public function tearDown()
53
    {
54
        if ($this->client === null || !$this->client->doesBucketExist($this->bucket)) {
55
            return;
56
        }
57
58
        $result = $this->client->listObjects(['Bucket' => $this->bucket]);
59
        $staleObjects = $result->get('Contents');
60
61
        foreach ($staleObjects as $staleObject) {
62
            $this->client->deleteObject(['Bucket' => $this->bucket, 'Key' => $staleObject['Key']]);
63
        }
64
65
        $this->client->deleteBucket(['Bucket' => $this->bucket]);
66
    }
67
}
68