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

S3   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 77
rs 10
wmc 5
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getStorageId() 0 3 1
B readObject() 0 27 1
A writeObject() 0 7 1
A deleteObject() 0 6 1
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 OCP\Files\ObjectStore\IObjectStore;
25
26
// TODO: proper composer
27
set_include_path(get_include_path() . PATH_SEPARATOR .
28
	\OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php');
29
require_once 'aws-autoloader.php';
30
31
class S3 implements IObjectStore {
32
	use S3ConnectionTrait;
33
34
	public function __construct($parameters) {
35
		$this->parseParams($parameters);
36
	}
37
38
	/**
39
	 * @return string the container or bucket name where objects are stored
40
	 * @since 7.0.0
41
	 */
42
	function getStorageId() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
		return $this->id;
44
	}
45
46
	/**
47
	 * @param string $urn the unified resource name used to identify the object
48
	 * @return resource stream with the read data
49
	 * @throws \Exception when something goes wrong, message will be logged
50
	 * @since 7.0.0
51
	 */
52
	function readObject($urn) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
		// Create the command and serialize the request
54
		$request = $this->getConnection()->getCommand('GetObject', [
55
			'Bucket' => $this->bucket,
56
			'Key' => $urn
57
		])->prepare();
58
59
		$request->dispatch('request.before_send', array(
60
			'request' => $request
61
		));
62
63
		$headers = $request->getHeaderLines();
64
		$headers[] = 'Connection: close';
65
66
		$opts = [
67
			'http' => [
68
				'method' => "GET",
69
				'header' => $headers
70
			],
71
			'ssl' => [
72
				'verify_peer' => true
73
			]
74
		];
75
76
		$context = stream_context_create($opts);
77
		return fopen($request->getUrl(), 'r', false, $context);
78
	}
79
80
	/**
81
	 * @param string $urn the unified resource name used to identify the object
82
	 * @param resource $stream stream with the data to write
83
	 * @throws \Exception when something goes wrong, message will be logged
84
	 * @since 7.0.0
85
	 */
86
	function writeObject($urn, $stream) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
		$this->getConnection()->putObject([
88
			'Bucket' => $this->bucket,
89
			'Key' => $urn,
90
			'Body' => $stream
91
		]);
92
	}
93
94
	/**
95
	 * @param string $urn the unified resource name used to identify the object
96
	 * @return void
97
	 * @throws \Exception when something goes wrong, message will be logged
98
	 * @since 7.0.0
99
	 */
100
	function deleteObject($urn) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101
		$this->getConnection()->deleteObject([
102
			'Bucket' => $this->bucket,
103
			'Key' => $urn
104
		]);
105
	}
106
107
}
108