S3Adapter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 74
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 4 1
A getFilePath() 0 4 1
A getFileUrl() 0 4 1
A createClient() 0 13 1
1
<?php namespace Nord\Lumen\FileManager\Adapters;
2
3
use Aws\S3\S3Client;
4
use Nord\Lumen\FileManager\Contracts\File;
5
6
class S3Adapter extends DiskAdapter
7
{
8
9
    /**
10
     * @var S3Client
11
     */
12
    private $client;
13
14
    /**
15
     * @var string
16
     */
17
    private $bucket;
18
19
20
    /**
21
     * S3Adapter constructor.
22
     *
23
     * @param array $config
24
     */
25
    public function __construct(array $config)
26
    {
27
        parent::__construct($config);
28
29
        $this->client = $this->createClient($config);
30
        $this->bucket = array_get($config, 'bucket', env('S3_BUCKET'));
31
    }
32
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function getName()
38
    {
39
        return 's3';
40
    }
41
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getFilePath(File $file, array $options)
47
    {
48
        return $this->createFilePath($file);
49
    }
50
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getFileUrl(File $file, array $options)
56
    {
57
        return $this->client->getObjectUrl($this->bucket, $this->createFilePath($file));
58
    }
59
60
61
    /**
62
     * @param array $config
63
     *
64
     * @return S3Client
65
     */
66
    private function createClient(array $config)
67
    {
68
        $key     = array_get($config, 'key', env('S3_KEY'));
69
        $secret  = array_get($config, 'secret', env('S3_SECRET'));
70
        $region  = array_get($config, 'region', env('S3_REGION'));
71
        $version = array_get($config, 'version', 'latest');
72
73
        return new S3Client([
74
            'credentials' => ['key' => $key, 'secret' => $secret],
75
            'region'      => $region,
76
            'version'     => $version,
77
        ]);
78
    }
79
}
80