Completed
Push — master ( 144614...3a2d63 )
by Albin
13s
created

AwsS3SetUpTearDownTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 20 4
A tearDown() 0 15 4
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
        $this->client = new S3Client([
31
            'region'      => $region ? $region : 'eu-west-1',
32
            'version'     => 'latest',
33
            'credentials' => [
34
                'key'    => $key,
35
                'secret' => $secret,
36
            ],
37
        ]);
38
    }
39
40
    public function tearDown()
41
    {
42
        if ($this->client === null || !$this->client->doesBucketExist($this->bucket)) {
43
            return;
44
        }
45
46
        $result = $this->client->listObjects(['Bucket' => $this->bucket]);
47
        $staleObjects = $result->get('Contents');
48
49
        foreach ($staleObjects as $staleObject) {
50
            $this->client->deleteObject(['Bucket' => $this->bucket, 'Key' => $staleObject['Key']]);
51
        }
52
53
        $this->client->deleteBucket(['Bucket' => $this->bucket]);
54
    }
55
}
56