Passed
Push — master ( b69b17...32a6f4 )
by Morris
11:33
created

InMemoryFile::putContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @copyright Copyright (c) 2018, Michael Weimann <[email protected]>
6
 *
7
 * @author Michael Weimann <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 */
23
24
namespace OCP\Files\SimpleFS;
25
26
use OCP\Files\NotPermittedException;
27
28
/**
29
 * This class represents a file that is only hold in memory.
30
 *
31
 * @package OC\Files\SimpleFS
32
 * @since 16.0.0
33
 */
34
class InMemoryFile implements ISimpleFile {
35
	/**
36
	 * Holds the file name.
37
	 *
38
	 * @var string
39
	 */
40
	private $name;
41
42
	/**
43
	 * Holds the file contents.
44
	 *
45
	 * @var string
46
	 */
47
	private $contents;
48
49
	/**
50
	 * InMemoryFile constructor.
51
	 *
52
	 * @param string $name The file name
53
	 * @param string $contents The file contents
54
	 * @since 16.0.0
55
	 */
56
	public function __construct(string $name, string $contents) {
57
		$this->name = $name;
58
		$this->contents = $contents;
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 * @since 16.0.0
64
	 */
65
	public function getName() {
66
		return $this->name;
67
	}
68
69
	/**
70
	 * @inheritdoc
71
	 * @since 16.0.0
72
	 */
73
	public function getSize() {
74
		return strlen($this->contents);
75
	}
76
77
	/**
78
	 * @inheritdoc
79
	 * @since 16.0.0
80
	 */
81
	public function getETag() {
82
		return '';
83
	}
84
85
	/**
86
	 * @inheritdoc
87
	 * @since 16.0.0
88
	 */
89
	public function getMTime() {
90
		return time();
91
	}
92
93
	/**
94
	 * @inheritdoc
95
	 * @since 16.0.0
96
	 */
97
	public function getContent() {
98
		return $this->contents;
99
	}
100
101
	/**
102
	 * @inheritdoc
103
	 * @since 16.0.0
104
	 */
105
	public function putContent($data) {
106
		$this->contents = $data;
107
	}
108
109
	/**
110
	 * In memory files can't be deleted.
111
	 *
112
	 * @since 16.0.0
113
	 */
114
	public function delete() {
115
		// unimplemented for in memory files
116
	}
117
118
	/**
119
	 * @inheritdoc
120
	 * @since 16.0.0
121
	 */
122
	public function getMimeType() {
123
		$fileInfo = new \finfo(FILEINFO_MIME_TYPE);
124
		return $fileInfo->buffer($this->contents);
125
	}
126
127
	/**
128
	 * Stream reading is unsupported for in memory files.
129
	 *
130
	 * @throws NotPermittedException
131
	 * @since 16.0.0
132
	 */
133
	public function read() {
134
		throw new NotPermittedException(
135
			'Stream reading is unsupported for in memory files'
136
		);
137
	}
138
139
	/**
140
	 * Stream writing isn't available for in memory files.
141
	 *
142
	 * @throws NotPermittedException
143
	 * @since 16.0.0
144
	 */
145
	public function write() {
146
		throw new NotPermittedException(
147
			'Stream writing is unsupported for in memory files'
148
		);
149
	}
150
}
151