Passed
Pull Request — 2.x (#730)
by Antonio Carlos
06:39
created

Aws::filesystemFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace A17\Twill\Services\Cloud;
4
5
use Aws\S3\S3Client;
6
use Illuminate\Support\Str;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\AwsS3v3\AwsS3Adapter;
9
10
class Aws
11
{
12
    public function filesystemFactory($source)
13
    {
14
        $config = $this->getConfigFor($source);
15
16
        $client = new S3Client($config);
17
18
        $adapter = new AwsS3Adapter($client, $config['bucket'], $config['root']);
19
20
        return new Filesystem($adapter);
21
    }
22
23
    public function getConfigFor($disk)
24
    {
25
        return [
26
            'credentials' => [
27
                'key' => $this->config($disk, 'key'),
28
29
                'secret' => $this->config($disk, 'secret'),
30
            ],
31
32
            'region' => $this->config($disk, 'region'),
33
34
            'root' => $this->config($disk, 'root', ''),
35
36
            'bucket' => $this->config($disk, 'bucket'),
37
38
            'version' => $this->config($disk, 'version', 'latest'),
39
        ];
40
    }
41
42
    public function config($disk, $key, $default = null)
43
    {
44
        $env1 = Str::upper(Str::snake($disk));
45
46
        $env2 = $env1 === 'AWS' ? 'S3' : 'AWS';
47
48
        $envSuffix = Str::upper($key);
49
50
        if (filled($value = config("filesystems.disks.{$disk}.{$key}"))) {
51
            return $value;
52
        }
53
54
        return env("{$env1}_{$envSuffix}", env("{$env2}_{$envSuffix}", $default));
55
    }
56
}
57