Completed
Push — master ( e74dad...d99fd7 )
by WEBEWEB
04:05
created

FileUtility::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility;
13
14
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
15
use WBW\Library\Core\Exception\Directory\DirectoryNotFoundException;
16
use WBW\Library\Core\Exception\File\FileNotFoundException;
17
use WBW\Library\Core\File\FileSizeInterface;
18
19
/**
20
 * File utility.
21
 *
22
 * @author NdC/WBW <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Utility
24
 * @final
25
 */
26
final class FileUtility implements FileSizeInterface {
27
28
	/**
29
	 * Delete a file.
30
	 *
31
	 * @param string $filename The filename.
32
	 * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
33
	 */
34
	public static function delete($filename) {
35
		if (false === file_exists($filename)) {
36
			throw new FileNotFoundException($filename);
37
		}
38
		return unlink($filename);
39
	}
40
41
	/**
42
	 * Format a size.
43
	 *
44
	 * @param  $size The size.
45
	 * @param string $unit The unit.
46
	 * @return string Returns the formated size.
47
	 */
48
	public static function formatSize($size, $unit = null, $decimals = 2) {
49
50
51
		// Initialize the units.
52
		$units = self::getUnits();
53
54
		// Find the unit.
55
		$index = array_search($unit, $units);
56
		if (null !== $unit && false === $index) {
57
			throw new IllegalArgumentException("The unit \"" . $unit . "\" does not exists");
58
		}
59
60
		// Initialize the output.
61
		$output = $size;
62
63
		$iteration = 0;
64
		while (self::FILE_SIZE_DIVIDER <= $output || $iteration < $index) {
65
			$output /= self::FILE_SIZE_DIVIDER;
66
			++$iteration;
67
		}
68
69
		// Return the output.
70
		return implode(" ", [sprintf("%." . $decimals . "f", $output), $units[$iteration]]);
71
	}
72
73
	/**
74
	 * Get a file contents.
75
	 *
76
	 * @param string $filename The filename.
77
	 * @return string Returns the file contents.
78
	 * @throws FileNotFoundException Throws a file not found exception if the file does not exists.
79
	 */
80
	public static function getContents($filename) {
81
		if (false === file_exists($filename)) {
82
			throw new FileNotFoundException($filename);
83
		}
84
		return file_get_contents($filename);
85
	}
86
87
	/**
88
	 * Get the filenames.
89
	 *
90
	 * @param string $pathname The pathname.
91
	 * @param string $extension The file extension.
92
	 * @return array Returns the filenames.
93
	 * @throws FileNotFoundException Throws a file not found exception if the directory does not exists.
94
	 */
95
	public static function getFilenames($pathname, $extension = null) {
96
97
		// Check if the directory exists.
98
		if (false === file_exists($pathname)) {
99
			throw new DirectoryNotFoundException($pathname);
100
		}
101
102
		// Initialize the filenames.
103
		$filenames = [];
104
105
		// Open the directory.
106
		if (false !== ($directory = opendir($pathname))) {
107
108
			// Initialize the offset.
109
			$offset = strlen($extension);
110
111
			// Read the directory.
112
			while (($file = readdir($directory)) !== false) {
113
114
				// Determines if the file should be added.
115
				if ("." !== $file && ".." !== $file && ((null === $extension) || 0 === substr_compare($file, $extension, -$offset))) {
116
					$filenames[] = $file;
117
				}
118
			}
119
120
			// Close the directory.
121
			closedir($directory);
122
		}
123
124
		// Return the filenames.
125
		return $filenames;
126
	}
127
128
	/**
129
	 * Get a file size.
130
	 *
131
	 * @param string $filename The filename.
132
	 * @return integer Returns the file size.
133
	 * @throws FileNotFoundException Throws a File not found exception if the file does not exists.
134
	 */
135
	public static function getSize($filename) {
136
		if (false === file_exists($filename)) {
137
			throw new FileNotFoundException($filename);
138
		}
139
		clearstatcache();
140
		return filesize($filename);
141
	}
142
143
	/**
144
	 * Get the units.
145
	 *
146
	 * @return array Returns the units.
147
	 */
148
	public static function getUnits() {
149
		return [
150
			self::FILE_SIZE_UNIT_B,
151
			self::FILE_SIZE_UNIT_KB,
152
			self::FILE_SIZE_UNIT_MB,
153
			self::FILE_SIZE_UNIT_GB,
154
			self::FILE_SIZE_UNIT_TB,
155
			self::FILE_SIZE_UNIT_PB,
156
			self::FILE_SIZE_UNIT_EB,
157
			self::FILE_SIZE_UNIT_ZB,
158
			self::FILE_SIZE_UNIT_YB,
159
		];
160
	}
161
162
}
163