Completed
Push — master ( 6c11c5...8b9ad4 )
by Robin
22:29 queued 14:09
created

S3ConnectionTrait::parseParams()   B

Complexity

Conditions 11
Paths 41

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 12
c 1
b 0
f 0
nc 41
nop 1
dl 0
loc 17
rs 7.1162

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
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Files\ObjectStore;
23
24
use Aws\S3\Exception\S3Exception;
25
use Aws\S3\S3Client;
26
27
trait S3ConnectionTrait {
28
	/** @var array */
29
	protected $params;
30
31
	/** @var S3Client */
32
	protected $connection;
33
34
	/** @var string */
35
	protected $id;
36
37
	/** @var string */
38
	protected $bucket;
39
40
	/** @var int */
41
	protected $timeout;
42
43
	protected $test;
44
45
	protected function parseParams($params) {
46
		if (empty($params['key']) || empty($params['secret']) || empty($params['bucket'])) {
47
			throw new \Exception("Access Key, Secret and Bucket have to be configured.");
48
		}
49
50
		$this->id = 'amazon::' . $params['bucket'];
51
52
		$this->test = isset($params['test']);
53
		$this->bucket = $params['bucket'];
54
		$this->timeout = (!isset($params['timeout'])) ? 15 : $params['timeout'];
55
		$params['region'] = empty($params['region']) ? 'eu-west-1' : $params['region'];
56
		$params['hostname'] = empty($params['hostname']) ? 's3.amazonaws.com' : $params['hostname'];
57
		if (!isset($params['port']) || $params['port'] === '') {
58
			$params['port'] = (isset($params['use_ssl']) && $params['use_ssl'] === false) ? 80 : 443;
59
		}
60
		$this->params = $params;
61
	}
62
63
64
	/**
65
	 * Returns the connection
66
	 *
67
	 * @return S3Client connected client
68
	 * @throws \Exception if connection could not be made
69
	 */
70
	protected function getConnection() {
71
		if (!is_null($this->connection)) {
72
			return $this->connection;
73
		}
74
75
		$scheme = (isset($this->params['use_ssl']) && $this->params['use_ssl'] === false) ? 'http' : 'https';
76
		$base_url = $scheme . '://' . $this->params['hostname'] . ':' . $this->params['port'] . '/';
77
78
		$options = [
79
			'key' => $this->params['key'],
80
			'secret' => $this->params['secret'],
81
			'base_url' => $base_url,
82
			'region' => $this->params['region'],
83
			S3Client::COMMAND_PARAMS => [
84
				'PathStyle' => isset($this->params['use_path_style']) ? $this->params['use_path_style'] : false,
85
			]
86
		];
87
		if (isset($this->params['proxy'])) {
88
			$options[S3Client::REQUEST_OPTIONS] = ['proxy' => $this->params['proxy']];
89
		}
90
		$this->connection = S3Client::factory($options);
91
92
		if (!$this->connection->isValidBucketName($this->bucket)) {
93
			throw new \Exception("The configured bucket name is invalid.");
94
		}
95
96
		if (!$this->connection->doesBucketExist($this->bucket)) {
97
			try {
98
				$this->connection->createBucket(array(
99
					'Bucket' => $this->bucket
100
				));
101
				$this->connection->waitUntilBucketExists(array(
102
					'Bucket' => $this->bucket,
103
					'waiter.interval' => 1,
104
					'waiter.max_attempts' => 15
105
				));
106
				$this->testTimeout();
107
			} catch (S3Exception $e) {
0 ignored issues
show
Bug introduced by
The class Aws\S3\Exception\S3Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
108
				\OCP\Util::logException('files_external', $e);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\Util::logException() has been deprecated with message: 8.2.0 use logException of \OCP\ILogger

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
				throw new \Exception('Creation of bucket failed. ' . $e->getMessage());
110
			}
111
		}
112
113
		return $this->connection;
114
	}
115
116
	/**
117
	 * when running the tests wait to let the buckets catch up
118
	 */
119
	private function testTimeout() {
120
		if ($this->test) {
121
			sleep($this->timeout);
122
		}
123
	}
124
}
125