AwsS3Filesystem::init()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 17
rs 9.6111
1
<?php
2
/**
3
 * @link https://github.com/acidwave/yii2-flysystem
4
 * @copyright Copyright (c) 2021 Acid Wave
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace Acidwave\Flysystem;
9
10
use Aws\S3\S3Client;
11
use League\Flysystem\Visibility;
12
use yii\base\InvalidConfigException;
13
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
14
use League\Flysystem\AwsS3V3\PortableVisibilityConverter;
15
16
/**
17
 * AwsS3Filesystem
18
 *
19
 * @author Acid Wave <[email protected]>
20
 */
21
class AwsS3Filesystem extends Filesystem
22
{
23
    /**
24
     * @var string
25
     */
26
    public $key;
27
    /**
28
     * @var string
29
     */
30
    public $secret;
31
    /**
32
     * @var string
33
     */
34
    public $region;
35
    /**
36
     * @var string
37
     */
38
    public $baseUrl;
39
    /**
40
     * @var string
41
     */
42
    public $version;
43
    /**
44
     * @var string
45
     */
46
    public $bucket;
47
    /**
48
     * @var string|null
49
     */
50
    public $prefix;
51
    /**
52
     * @var bool
53
     */
54
    public $pathStyleEndpoint = false;
55
    /**
56
     * @var string
57
     */
58
    public $visibility = Visibility::PUBLIC;
59
    /**
60
     * @var bool
61
     */
62
    public $streamReads = false;
63
    /**
64
     * @var string
65
     */
66
    public $endpoint;
67
    /**
68
     * @var array|\Aws\CacheInterface|\Aws\Credentials\CredentialsInterface|bool|callable
69
     */
70
    public $credentials;
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function init()
76
    {
77
        if ($this->credentials === null) {
78
            if ($this->key === null) {
79
                throw new InvalidConfigException('The "key" property must be set.');
80
            }
81
82
            if ($this->secret === null) {
83
                throw new InvalidConfigException('The "secret" property must be set.');
84
            }
85
        }
86
87
        if ($this->bucket === null) {
88
            throw new InvalidConfigException('The "bucket" property must be set.');
89
        }
90
91
        parent::init();
92
    }
93
94
    /**
95
     * @return AwsS3V3Adapter
96
     */
97
    protected function prepareAdapter()
98
    {
99
        $config = [];
100
101
        if ($this->credentials === null) {
102
            $config['credentials'] = ['key' => $this->key, 'secret' => $this->secret];
103
        } else {
104
            $config['credentials'] = $this->credentials;
105
        }
106
107
108
        if ($this->pathStyleEndpoint === true) {
109
            $config['use_path_style_endpoint'] = true;
110
        }
111
112
        if ($this->region !== null) {
113
            $config['region'] = $this->region;
114
        }
115
116
        if ($this->baseUrl !== null) {
117
            $config['base_url'] = $this->baseUrl;
118
        }
119
120
        if ($this->endpoint !== null) {
121
            $config['endpoint'] = $this->endpoint;
122
        }
123
124
        $config['version'] = (($this->version !== null) ? $this->version : 'latest');
125
126
        $client = new S3Client($config);
127
128
        return new AwsS3V3Adapter(
129
            $client,
130
            $this->bucket,
131
            $this->prefix,
0 ignored issues
show
Bug introduced by
It seems like $this->prefix can also be of type null; however, parameter $prefix of League\Flysystem\AwsS3V3...3Adapter::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            /** @scrutinizer ignore-type */ $this->prefix,
Loading history...
132
            new PortableVisibilityConverter(
133
                $this->visibility
134
            )
135
        );
136
    }
137
}
138