Completed
Push — stable8.2 ( 5200ba...61a71c )
by
unknown
11:10
created

Quota::isPartFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 1
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author Jörn Friedrich Dreyer <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Vincent Petry <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2015, ownCloud, Inc.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Files\Storage\Wrapper;
27
28
class Quota extends Wrapper {
29
30
	/**
31
	 * @var int $quota
32
	 */
33
	protected $quota;
34
35
	/**
36
	 * @var string $sizeRoot
37
	 */
38
	protected $sizeRoot;
39
40
	/**
41
	 * @param array $parameters
42
	 */
43 96
	public function __construct($parameters) {
44 96
		$this->storage = $parameters['storage'];
45 96
		$this->quota = $parameters['quota'];
46 96
		$this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
47 96
	}
48
49
	/**
50
	 * @return int quota value
51
	 */
52 3
	public function getQuota() {
53 3
		return $this->quota;
54
	}
55
56
	/**
57
	 * @param string $path
58
	 * @param \OC\Files\Storage\Storage $storage
59
	 */
60 78
	protected function getSize($path, $storage = null) {
61 78
		if (is_null($storage)) {
62 78
			$cache = $this->getCache();
63 78
		} else {
64
			$cache = $storage->getCache();
65
		}
66 78
		$data = $cache->get($path);
67 78
		if (is_array($data) and isset($data['size'])) {
68 16
			return $data['size'];
69
		} else {
70 62
			return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
71
		}
72
	}
73
74
	/**
75
	 * Get free space as limited by the quota
76
	 *
77
	 * @param string $path
78
	 * @return int
79
	 */
80 78
	public function free_space($path) {
81 78
		if ($this->quota < 0) {
82
			return $this->storage->free_space($path);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->storage->free_space($path); of type integer|false adds false to the return on line 82 which is incompatible with the return type documented by OC\Files\Storage\Wrapper\Quota::free_space of type integer. It seems like you forgot to handle an error condition.
Loading history...
83
		} else {
84 78
			$used = $this->getSize($this->sizeRoot);
85 78
			if ($used < 0) {
86 62
				return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
87
			} else {
88 16
				$free = $this->storage->free_space($path);
89 16
				$quotaFree = max($this->quota - $used, 0);
0 ignored issues
show
Bug Compatibility introduced by
The expression max($this->quota - $used, 0); of type integer|double adds the type double to the return on line 96 which is incompatible with the return type declared by the interface OCP\Files\Storage::free_space of type integer|false.
Loading history...
90
				// if free space is known
91 16
				if ($free >= 0) {
92 15
					$free = min($free, $quotaFree);
0 ignored issues
show
Bug Compatibility introduced by
The expression min($free, $quotaFree); of type integer|false|double adds the type double to the return on line 96 which is incompatible with the return type declared by the interface OCP\Files\Storage::free_space of type integer|false.
Loading history...
93 15
				} else {
94 1
					$free = $quotaFree;
95
				}
96 16
				return $free;
97
			}
98
		}
99
	}
100
101
	/**
102
	 * see http://php.net/manual/en/function.file_put_contents.php
103
	 *
104
	 * @param string $path
105
	 * @param string $data
106
	 * @return bool
107
	 */
108 62
	public function file_put_contents($path, $data) {
109 62
		$free = $this->free_space('');
110 62
		if ($free < 0 or strlen($data) < $free) {
111 61
			return $this->storage->file_put_contents($path, $data);
112
		} else {
113 1
			return false;
114
		}
115
	}
116
117
	/**
118
	 * see http://php.net/manual/en/function.copy.php
119
	 *
120
	 * @param string $source
121
	 * @param string $target
122
	 * @return bool
123
	 */
124 21 View Code Duplication
	public function copy($source, $target) {
125 21
		$free = $this->free_space('');
126 21
		if ($free < 0 or $this->getSize($source) < $free) {
127 20
			return $this->storage->copy($source, $target);
128
		} else {
129 1
			return false;
130
		}
131
	}
132
133
	/**
134
	 * see http://php.net/manual/en/function.fopen.php
135
	 *
136
	 * @param string $path
137
	 * @param string $mode
138
	 * @return resource
139
	 */
140 8
	public function fopen($path, $mode) {
141 8
		$source = $this->storage->fopen($path, $mode);
142 8
143 8
		// don't apply quota for part files
144
		if (!$this->isPartFile($path)) {
145 6
			$free = $this->free_space('');
146 5
			if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
147
				// only apply quota for files, not metadata, trash or others
148 1
				if (strpos(ltrim($path, '/'), 'files/') === 0) {
149 4
					return \OC\Files\Stream\Quota::wrap($source, $free);
150
				}
151
			}
152
		}
153
		return $source;
154
	}
155
156
	/**
157
	 * Checks whether the given path is a part file
158 8
	 *
159 8
	 * @param string $path Path that may identify a .part file
160 8
	 * @return string File path without .part extension
161 8
	 * @note this is needed for reusing keys
162
	 */
163
	private function isPartFile($path) {
164
		$extension = pathinfo($path, PATHINFO_EXTENSION);
165
166
		return ($extension === 'part');
167
	}
168
169
	/**
170
	 * @param \OCP\Files\Storage $sourceStorage
171
	 * @param string $sourceInternalPath
172
	 * @param string $targetInternalPath
173
	 * @return bool
174
	 */
175 View Code Duplication
	public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
176
		$free = $this->free_space('');
177
		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
0 ignored issues
show
Documentation introduced by
$sourceStorage is of type object<OCP\Files\Storage>, but the function expects a object<OC\Files\Storage\Storage>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
			return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
179
		} else {
180
			return false;
181
		}
182
	}
183
184
	/**
185
	 * @param \OCP\Files\Storage $sourceStorage
186
	 * @param string $sourceInternalPath
187
	 * @param string $targetInternalPath
188
	 * @return bool
189
	 */
190 View Code Duplication
	public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
191
		$free = $this->free_space('');
192
		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
0 ignored issues
show
Documentation introduced by
$sourceStorage is of type object<OCP\Files\Storage>, but the function expects a object<OC\Files\Storage\Storage>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
			return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
194
		} else {
195
			return false;
196
		}
197
	}
198
}
199