Completed
Push — master ( e44baf...dee4cd )
by Gabriel
12:09 queued 11s
created

HasCloudDriverTrait::formatS3Config()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.8666
ccs 0
cts 7
cp 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Nip\Filesystem\FilesystemManager;
4
5
use Aws\S3\S3Client;
6
use League\Flysystem\AwsS3v3\AwsS3Adapter as S3Adapter;
7
use League\Flysystem\FilesystemInterface;
8
use Nip\Filesystem\FileDisk;
9
10
/**
11
 * Trait HasCloudDriverTrait
12
 * @package Nip\Filesystem\FilesystemManager
13
 */
14
trait HasCloudDriverTrait
15
{
16
    /**
17
     * Create an instance of the Amazon S3 driver.
18
     *
19
     * @param array $config
20
     * @return FilesystemInterface|FileDisk
21
     */
22
    public function createS3Driver(array $config)
23
    {
24
        $s3Config = $this->formatS3Config($config);
25
        $root = $s3Config['root'] ?? null;
26
        $options = $config['options'] ?? [];
27
28
        return $this->adapt($this->createDisk(
0 ignored issues
show
Bug introduced by
It seems like createDisk() 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...
Bug introduced by
It seems like adapt() 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...
29
            new S3Adapter(new S3Client($s3Config), $s3Config['bucket'], $root, $options),
30
            $config
31
        ));
32
    }
33
34
    /**
35
     * Format the given S3 configuration with the default options.
36
     *
37
     * @param array $config
38
     * @return array
39
     */
40
    protected function formatS3Config(array $config)
41
    {
42
        $config += ['version' => 'latest'];
43
        if ($config['key'] && $config['secret']) {
44
            $config['credentials'] = [
45
                'key' => $config['key'],
46
                'secret' => $config['secret'],
47
            ];
48
        }
49
50
        return $config;
51
    }
52
}
53