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

StorageObjectStore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use OCP\Files\Storage\IStorage;
26
27
/**
28
 * Object store that wraps a storage backend, mostly for testing purposes
29
 */
30
class StorageObjectStore implements IObjectStore {
31
	/** @var IStorage */
32
	private $storage;
33
34
	/**
35
	 * @param IStorage $storage
36
	 */
37
	public function __construct(IStorage $storage) {
38
		$this->storage = $storage;
39
	}
40
41
	/**
42
	 * @return string the container or bucket name where objects are stored
43
	 * @since 7.0.0
44
	 */
45
	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...
46
		$this->storage->getId();
47
	}
48
49
	/**
50
	 * @param string $urn the unified resource name used to identify the object
51
	 * @return resource stream with the read data
52
	 * @throws \Exception when something goes wrong, message will be logged
53
	 * @since 7.0.0
54
	 */
55
	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...
56
		$handle = $this->storage->fopen($urn, 'r');
57
		if ($handle) {
58
			return $handle;
59
		} else {
60
			throw new \Exception();
61
		}
62
	}
63
64
	/**
65
	 * @param string $urn the unified resource name used to identify the object
66
	 * @param resource $stream stream with the data to write
67
	 * @throws \Exception when something goes wrong, message will be logged
68
	 * @since 7.0.0
69
	 */
70
	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...
71
		$handle = $this->storage->fopen($urn, 'w');
72
		if ($handle) {
73
			stream_copy_to_stream($stream, $handle);
74
			fclose($handle);
75
		} else {
76
			throw new \Exception();
77
		}
78
	}
79
80
	/**
81
	 * @param string $urn the unified resource name used to identify the object
82
	 * @return void
83
	 * @throws \Exception when something goes wrong, message will be logged
84
	 * @since 7.0.0
85
	 */
86
	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...
87
		$this->storage->unlink($urn);
88
	}
89
90
}
91