Passed
Push — master ( 892589...8bee8e )
by Roeland
12:32 queued 11s
created

S3ConnectionTrait::parseParams()   C

Complexity

Conditions 12
Paths 81

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 12
nc 81
nop 1
dl 0
loc 17
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Robin Appelman <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author S. Cat <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OC\Files\ObjectStore;
29
30
use Aws\ClientResolver;
31
use Aws\S3\Exception\S3Exception;
32
use Aws\S3\S3Client;
33
use OCP\ILogger;
34
35
trait S3ConnectionTrait {
36
	/** @var array */
37
	protected $params;
38
39
	/** @var S3Client */
40
	protected $connection;
41
42
	/** @var string */
43
	protected $id;
44
45
	/** @var string */
46
	protected $bucket;
47
48
	/** @var int */
49
	protected $timeout;
50
	
51
	/** @var int */
52
	protected $uploadPartSize;
53
54
	protected $test;
55
56
	protected function parseParams($params) {
57
		if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
58
			throw new \Exception("Access Key, Secret and Bucket have to be configured.");
59
		}
60
61
		$this->id = 'amazon::' . $params['bucket'];
62
63
		$this->test = isset($params['test']);
64
		$this->bucket = $params['bucket'];
65
		$this->timeout = !isset($params['timeout']) ? 15 : $params['timeout'];
66
		$this->uploadPartSize = !isset($params['uploadPartSize']) ? 524288000 : $params['uploadPartSize'];
67
		$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
68
		$params['hostname'] = empty($params['hostname']) ? 's3.' . $params['region'] . '.amazonaws.com' : $params['hostname'];
69
		if (!isset($params['port']) || $params['port'] === '') {
70
			$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
71
		}
72
		$this->params = $params;
73
	}
74
75
	public function getBucket() {
76
		return $this->bucket;
77
	}
78
79
	/**
80
	 * Returns the connection
81
	 *
82
	 * @return S3Client connected client
83
	 * @throws \Exception if connection could not be made
84
	 */
85
	public function getConnection() {
86
		if (!is_null($this->connection)) {
87
			return $this->connection;
88
		}
89
90
		$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
91
		$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
92
93
		$options = [
94
			'version' => isset($this->params['version']) ? $this->params['version'] : 'latest',
95
			'credentials' => [
96
				'key' => $this->params['key'],
97
				'secret' => $this->params['secret'],
98
			],
99
			'endpoint' => $base_url,
100
			'region' => $this->params['region'],
101
			'use_path_style_endpoint' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
102
			'signature_provider' => \Aws\or_chain([self::class, 'legacySignatureProvider'], ClientResolver::_default_signature_provider())
103
		];
104
		if (isset($this->params['proxy'])) {
105
			$options['request.options'] = ['proxy' => $this->params['proxy']];
106
		}
107
		if (isset($this->params['legacy_auth']) && $this->params['legacy_auth']) {
108
			$options['signature_version'] = 'v2';
109
		}
110
		$this->connection = new S3Client($options);
111
112
		if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
113
			$logger = \OC::$server->getLogger();
114
			$logger->debug('Bucket "' . $this->bucket . '" This bucket name is not dns compatible, it may contain invalid characters.',
115
					 ['app' => 'objectstore']);
116
		}
117
118
		if (!$this->connection->doesBucketExist($this->bucket)) {
119
			$logger = \OC::$server->getLogger();
120
			try {
121
				$logger->info('Bucket "' . $this->bucket . '" does not exist - creating it.', ['app' => 'objectstore']);
122
				if (!$this->connection->isBucketDnsCompatible($this->bucket)) {
123
					throw new \Exception("The bucket will not be created because the name is not dns compatible, please correct it: " . $this->bucket);
124
				}
125
				$this->connection->createBucket(['Bucket' => $this->bucket]);
126
				$this->testTimeout();
127
			} catch (S3Exception $e) {
128
				$logger->logException($e, [
129
					'message' => 'Invalid remote storage.',
130
					'level' => ILogger::DEBUG,
131
					'app' => 'objectstore',
132
				]);
133
				throw new \Exception('Creation of bucket "' . $this->bucket . '" failed. ' . $e->getMessage());
134
			}
135
		}
136
137
		// google cloud's s3 compatibility doesn't like the EncodingType parameter
138
		if (strpos($base_url, 'storage.googleapis.com')) {
139
			$this->connection->getHandlerList()->remove('s3.auto_encode');
140
		}
141
142
		return $this->connection;
143
	}
144
145
	/**
146
	 * when running the tests wait to let the buckets catch up
147
	 */
148
	private function testTimeout() {
149
		if ($this->test) {
150
			sleep($this->timeout);
151
		}
152
	}
153
154
	public static function legacySignatureProvider($version, $service, $region) {
0 ignored issues
show
Unused Code introduced by
The parameter $service is not used and could be removed. ( Ignorable by Annotation )

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

154
	public static function legacySignatureProvider($version, /** @scrutinizer ignore-unused */ $service, $region) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $region is not used and could be removed. ( Ignorable by Annotation )

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

154
	public static function legacySignatureProvider($version, $service, /** @scrutinizer ignore-unused */ $region) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
155
		switch ($version) {
156
			case 'v2':
157
			case 's3':
158
				return new S3Signature();
159
			default:
160
				return null;
161
		}
162
	}
163
}
164