Completed
Push — develop ( 88e90e...a239dd )
by Michael
02:36
created

File::length()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 3
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\File;
12
13
/**
14
 * The file class.
15
 *
16
 * @since 1.0.0
17
 */
18
class File implements \Countable
19
{
20
	const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
21
22
	/** @var string the file path. */
23
	private $path;
24
25
	/**
26
	 * Constructs a File object with the given path.
27
	 *
28
	 * @param string $path
29
	 */
30 33
	public function __construct($path)
31
	{
32 33
		if (substr($path, -1) === static::DIRECTORY_SEPARATOR) {
33 1
			$this->path = substr($path, 0, -1);
34 1
		} else {
35 33
			$this->path = $path;
36
		}
37 33
	}
38
39
	/**
40
	 * Returns the string representation of the File object.
41
	 *
42
	 * @return string the string representation of the File object.
43
	 */
44 2
	public function __toString()
45
	{
46 2
		return $this->getPath();
47
	}
48
49
	/**
50
	 * Returns the path of the file.
51
	 *
52
	 * @return string the path of the file.
53
	 */
54 5
	public function getPath()
55
	{
56 5
		return $this->path;
57
	}
58
59
	/**
60
	 * Returns the parent directory of the file.
61
	 *
62
	 * @return string the parent directory of the file.
63
	 */
64 2
	public function getDirectory()
65
	{
66 2
		return dirname($this->path);
67
	}
68
69
	/**
70
	 * Returns the name of the file.
71
	 *
72
	 * @return string the name of the file.
73
	 */
74 1
	public function getName()
75
	{
76 1
		return basename($this->path);
77
	}
78
79
	/**
80
	 * Returns true if the file exists.
81
	 *
82
	 * @return bool true if the file exists.
83
	 */
84 8
	public function exists()
85
	{
86 8
		return file_exists($this->path);
87
	}
88
89
	/**
90
	 * Returns true if you can execute the file.
91
	 *
92
	 * @return bool true if you can execute the file.
93
	 */
94 1
	public function canExecute()
95
	{
96 1
		return is_executable($this->path);
97
	}
98
99
	/**
100
	 * Returns true if you can read the file.
101
	 *
102
	 * @return bool true if you can read the file.
103
	 */
104 1
	public function canRead()
105
	{
106 1
		return is_readable($this->path);
107
	}
108
109
	/**
110
	 * Returns true if you can write the file.
111
	 *
112
	 * @return bool true if you can write the file.
113
	 */
114 1
	public function canWrite()
115
	{
116 1
		return is_writeable($this->path);
117
	}
118
119
	/**
120
	 * Returns true if the file is a file.
121
	 *
122
	 * @return bool true if the file is a file.
123
	 */
124 2
	public function isFile()
125
	{
126 2
		return is_file($this->path);
127
	}
128
129
	/**
130
	 * Returns true if the file is a directory.
131
	 *
132
	 * @return bool true if the file is a directory.
133
	 */
134 5
	public function isDirectory()
135
	{
136 5
		return is_dir($this->path);
137
	}
138
139
	/**
140
	 * Returns the numer of bytes in the file, or -1 on failure.
141
	 *
142
	 * @return int the number of bytes in the file, or -1 on failure.
143
	 */
144 1
	public function count()
145
	{
146 1
		return $this->length();
147
	}
148
149
	/**
150
	 * Returns the numer of bytes in the file, or -1 on failure.
151
	 *
152
	 * @return int the number of bytes in the file, or -1 on failure.
153
	 */
154 2 View Code Duplication
	public function length()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
	{
156 2
		if (!$this->exists()) {
157 2
			return -1;
158
		}
159
160 2
		return ($result = filesize($this->path)) !== false ? $result : -1;
161
	}
162
163
	/**
164
	 * Returns the time of the last modification as a unixtimestap, or -1 on failure.
165
	 *
166
	 * @return int the time of the last modification as a unixtimestap, or -1 on failure.
167
	 */
168 1 View Code Duplication
	public function lastModified()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
	{
170 1
		if (!$this->exists()) {
171 1
			return -1;
172
		}
173
174 1
		return ($result = filemtime($this->path)) !== false ? $result : -1;
175
	}
176
177
	/**
178
	 * Returns an iterator with the files and directories in the current directory.
179
	 *
180
	 * @param bool $recursive = false
181
	 * @param bool $showHidden = false
182
	 * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
183
	 */
184 4
	private function listAllIterator($recursive = false, $showHidden = false)
185
	{
186 4
		if (!$this->isDirectory()) {
187 4
			return new \ArrayIterator([]);
188
		}
189
190 4
		$flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
191
192 4
		if (!$showHidden) {
193 4
			$flags = $flags | \FilesystemIterator::SKIP_DOTS;
194 4
		}
195
196 4
		if ($recursive) {
197 1
			return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
198
		}
199
200 3
		return new \FilesystemIterator($this->path, $flags);
201
	}
202
203
	/**
204
	 * Returns an array with the files and directories in the current directory.
205
	 *
206
	 * @param bool $recursive = false
207
	 * @param bool $showHidden = false
208
	 * @return string[] an array with the files and directories in the current directory.
209
	 */
210 2 View Code Duplication
	public function listAll($recursive = false, $showHidden = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
	{
212 2
		$result = [];
213
214 2
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
215 2
			$result[] = $element->getFilename();
216 2
		}
217
218 2
		return $result;
219
	}
220
221
	/**
222
	 * Returns an array with the directories in the current directory.
223
	 *
224
	 * @param bool $recursive = false
225
	 * @param bool $showHidden = false
226
	 * @return string[] an array with the directories in the current directory.
227
	 */
228 1 View Code Duplication
	public function listDirectories($recursive = false, $showHidden = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
229
	{
230 1
		$result = [];
231
232 1
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
233 1
			if ($element->isDir()) {
234 1
				$result[] = $element->getFilename();
235 1
			}
236 1
		}
237
238 1
		return $result;
239
	}
240
241
	/**
242
	 * Returns an array with the files in the current directory.
243
	 *
244
	 * @param bool $recursive = false
245
	 * @param bool $showHidden = false
246
	 * @return string[] an array with the files in the current directory.
247
	 */
248 1 View Code Duplication
	public function listFiles($recursive = false, $showHidden = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
	{
250 1
		$result = [];
251
252 1
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
253 1
			if ($element->isFile()) {
254 1
				$result[] = $element->getFilename();
255 1
			}
256 1
		}
257
258 1
		return $result;
259
	}
260
261
	/**
262
	 * Returns true if the file has been created.
263
	 *
264
	 * @param bool $override = false
265
	 * @return bool true if the file has been created.
266
	 */
267 1
	public function makeFile($override = false)
268
	{
269 1
		if ($this->exists() && !$override) {
270 1
			return false;
271
		}
272
273 1
		return file_put_contents($this->path, '') !== false;
274
	}
275
276
	/**
277
	 * Returns true if the directory has been created.
278
	 *
279
	 * @param bool $recursive = false
280
	 * @param int $permissions = 0755
281
	 * @return bool true if the directory has been created.
282
	 */
283 1
	public function makeDirectory($recursive = false, $permissions = 0775)
284
	{
285 1
		if ($this->exists()) {
286 1
			return false;
287
		}
288
289 1
		$old = umask(0777 - $permissions);
290 1
		$result = mkdir($this->path, $permissions, $recursive);
291 1
		umask($old);
292
293 1
		return $result;
294
	}
295
296
	/**
297
	 * Returns true if the file is succesfully moved.
298
	 *
299
	 * @param string $path
300
	 * @param bool $override = false
301
	 * @return bool true if the file is succesfully moved.
302
	 */
303 2
	public function move($path, $override = false)
304
	{
305 2
		if (!$this->exists()) {
306 2
			return false;
307
		}
308
309 2
		$file = new File($path);
310
311 2
		if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
312 1
			return false;
313
		}
314
315 2
		$this->path = $file->getPath();
316
317 2
		return true;
318
	}
319
320
	/**
321
	 * Returns true if the file is succesfully renamed.
322
	 *
323
	 * @param string $file
324
	 * @param bool $override = false
325
	 * @return bool true if the file is succesfully renamed.
326
	 */
327 1
	public function rename($file, $override = false)
328
	{
329 1
		return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
330
	}
331
332
	/**
333
	 * Returns true if the directory is succesfully removed.
334
	 *
335
	 * @param bool $recursive = false
336
	 * @return bool true if the directory is succesfully removed.
337
	 */
338 1
	public function removeDirectory($recursive = false)
339
	{
340 1
		if (!$recursive) {
341 1
			return rmdir($this->path);
342
		}
343
344 1
		foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
345 1
			$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
346 1
		}
347
348 1
		return true;
349
	}
350
351
	/**
352
	 * Returns true if the file is succesfully removed.
353
	 *
354
	 * @return bool true if the file is succesfully removed.
355
	 */
356 1
	public function removeFile()
357
	{
358 1
		if (!$this->isFile()) {
359 1
			return false;
360
		}
361
362 1
		return unlink($this->path);
363
	}
364
365
	/**
366
	 * Returns the content of the file.
367
	 *
368
	 * @return string the content of the file.
369
	 * @throws FileException on failure.
370
	 */
371 7
	public function read()
372
	{
373 7
		$result = file_get_contents($this->path);
374
375 7
		if ($result === false) {
376 2
			throw new FileException('Can\'t read the content.');
377
		}
378
379 5
		return $result;
380
	}
381
382
	/**
383
	 * Append the given content.
384
	 *
385
	 * @param string $content
386
	 * @return null
387
	 * @throws FileException on failure.
388
	 */
389 3
	public function append($content)
390
	{
391 3
		if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
392 1
			throw new FileException('Can\'t append the given content.');
393
		}
394 2
	}
395
396
	/**
397
	 * Write the given content.
398
	 *
399
	 * @param string $content
400
	 * @return null
401
	 * @throws FileException on failure.
402
	 */
403 3
	public function write($content)
404
	{
405 3
		if (file_put_contents($this->path, $content) === false) {
406 1
			throw new FileException('Can\'t write the given content.');
407
		}
408 2
	}
409
}
410