Completed
Push — master ( 0d138c...091bf0 )
by Lukas
07:48
created

FileAccessHelper::assertDirectoryExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OC\IntegrityCheck\Helpers;
24
25
/**
26
 * Class FileAccessHelper provides a helper around file_get_contents and
27
 * file_put_contents
28
 *
29
 * @package OC\IntegrityCheck\Helpers
30
 */
31
class FileAccessHelper {
32
	/**
33
	 * Wrapper around file_get_contents($filename, $data)
34
	 *
35
	 * @param string $filename
36
	 * @return string|false
37
	 */
38
	public function file_get_contents($filename) {
39
		return file_get_contents($filename);
40
	}
41
42
	/**
43
	 * Wrapper around file_exists($filename)
44
	 *
45
	 * @param string $filename
46
	 * @return bool
47
	 */
48
	public function file_exists($filename) {
49
		return file_exists($filename);
50
	}
51
52
	/**
53
	 * Wrapper around file_put_contents($filename, $data)
54
	 *
55
	 * @param string $filename
56
	 * @param string $data
57
	 * @return int
58
	 * @throws \Exception
59
	 */
60
	public function file_put_contents($filename, $data) {
61
		$bytesWritten = @file_put_contents($filename, $data);
62
		if ($bytesWritten === false || $bytesWritten !== strlen($data)){
63
			throw new \Exception('Failed to write into ' . $filename);
64
		}
65
		return $bytesWritten;
66
	}
67
68
	/**
69
	 * @param string $path
70
	 * @return bool
71
	 */
72
	public function is_writable($path) {
73
		return is_writable($path);
74
	}
75
76
	/**
77
	 * @param string $path
78
	 * @throws \Exception
79
	 */
80
	public function assertDirectoryExists($path) {
81
		if (!is_dir($path)) {
82
			throw new \Exception('Directory ' . $path . ' does not exist.');
83
		}
84
	}
85
}
86