Test Setup Failed
Push — develop ( 8e2eae...3f6661 )
by Berend
02:33
created

File::size()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
 */
9
10
namespace miBadger\File;
11
12
/**
13
 * The file class.
14
 *
15
 * @since 1.0.0
16
 */
17
class File implements \Countable
18
{
19
	const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
20
21
	/** @var string the file path. */
22
	private $path;
23
24
	/**
25
	 * Constructs a File object with the given path.
26
	 *
27
	 * @param string $path
28
	 */
29 33
	public function __construct($path)
30
	{
31 33
		if (mb_substr($path, -1) === static::DIRECTORY_SEPARATOR) {
32 1
			$this->path = mb_substr($path, 0, -1);
33
		} else {
34 33
			$this->path = $path;
35
		}
36 33
	}
37
38
	/**
39
	 * Returns the string representation of the File object.
40
	 *
41
	 * @return string the string representation of the File object.
42
	 */
43 2
	public function __toString()
44
	{
45 2
		return $this->getPath();
46
	}
47
48
	/**
49
	 * Returns the path of the file.
50
	 *
51
	 * @return string the path of the file.
52
	 */
53 5
	public function getPath()
54
	{
55 5
		return $this->path;
56
	}
57
58
	/**
59
	 * Returns the parent directory of the file.
60
	 *
61
	 * @return string the parent directory of the file.
62
	 */
63 2
	public function getDirectory()
64
	{
65 2
		return dirname($this->path);
66
	}
67
68
	/**
69
	 * Returns the name of the file.
70
	 *
71
	 * @return string the name of the file.
72
	 */
73 1
	public function getName()
74
	{
75 1
		return basename($this->path);
76
	}
77
78
	/**
79
	 * Returns the extension of the file
80
	 * 
81
	 * @return string the file extension
82
	 */
83 8
	public function getExtension()
84
	{
85 8
		return pathinfo($this->path, PATHINFO_EXTENSION);
86
	}
87
88
	/**
89
	 * Returns the mime-type as determined by information from php's magic.mime file, null on failure
90
	 * 
91
	 * @return string|null the mime type
92
	 */
93 1
	public function getMimeType()
94
	{
95 1
		$mime = mime_content_type($this->path);
96
		if ($mime === false) {
97
			return null;
98
		}
99
		return $mime;
100
	}
101
102
	/**
103 1
	 * Returns true if the file exists.
104
	 *
105 1
	 * @return bool true if the file exists.
106
	 */
107
	public function exists()
108
	{
109
		return file_exists($this->path);
110
	}
111
112
	/**
113 1
	 * Returns true if you can execute the file.
114
	 *
115 1
	 * @return bool true if you can execute the file.
116
	 */
117
	public function canExecute()
118
	{
119
		return is_executable($this->path);
120
	}
121
122
	/**
123 2
	 * Returns true if you can read the file.
124
	 *
125 2
	 * @return bool true if you can read the file.
126
	 */
127
	public function canRead()
128
	{
129
		return is_readable($this->path);
130
	}
131
132
	/**
133 5
	 * Returns true if you can write the file.
134
	 *
135 5
	 * @return bool true if you can write the file.
136
	 */
137
	public function canWrite()
138
	{
139
		return is_writeable($this->path);
140
	}
141
142
	/**
143 1
	 * Returns true if the file is a file.
144
	 *
145 1
	 * @return bool true if the file is a file.
146
	 */
147
	public function isFile()
148
	{
149
		return is_file($this->path);
150
	}
151
152
	/**
153 2
	 * Returns true if the file is a directory.
154
	 *
155 2
	 * @return bool true if the file is a directory.
156 2
	 */
157
	public function isDirectory()
158
	{
159 2
		return is_dir($this->path);
160
	}
161
162
	/**
163
	 * Returns the numer of bytes in the file, or -1 on failure.
164
	 *
165
	 * @return int the number of bytes in the file, or -1 on failure.
166
	 */
167 1
	public function count()
168
	{
169 1
		return $this->length();
170 1
	}
171
172
	/**
173 1
	 * Returns the numer of bytes in the file, or -1 on failure.
174
	 *
175
	 * @return int the number of bytes in the file, or -1 on failure.
176
	 */
177
	public function size()
178
	{
179
		return $this->length();
180
	}
181
182
	/**
183 4
	 * Returns the numer of bytes in the file, or -1 on failure.
184
	 *
185 4
	 * @return int the number of bytes in the file, or -1 on failure.
186 4
	 */
187 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...
188
	{
189 4
		if (!$this->exists()) {
190
			return -1;
191 4
		}
192 4
193
		return ($result = filesize($this->path)) !== false ? $result : -1;
194
	}
195 4
196 1
	/**
197
	 * Returns the time of the last modification as a unixtimestap, or -1 on failure.
198
	 *
199 3
	 * @return int the time of the last modification as a unixtimestap, or -1 on failure.
200
	 */
201 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...
202
	{
203
		if (!$this->exists()) {
204
			return -1;
205
		}
206
207
		return ($result = filemtime($this->path)) !== false ? $result : -1;
208
	}
209 2
210
	/**
211 2
	 * Returns an iterator with the files and directories in the current directory.
212
	 *
213 2
	 * @param bool $recursive = false
214 2
	 * @param bool $showHidden = false
215
	 * @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory.
216
	 */
217 2
	private function listAllIterator($recursive = false, $showHidden = false)
218
	{
219
		if (!$this->isDirectory()) {
220
			return new \ArrayIterator([]);
221
		}
222
223
		$flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO;
224
225
		if (!$showHidden) {
226
			$flags = $flags | \FilesystemIterator::SKIP_DOTS;
227 1
		}
228
229 1
		if ($recursive) {
230
			return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST);
231 1
		}
232 1
233 1
		return new \FilesystemIterator($this->path, $flags);
234
	}
235
236
	/**
237 1
	 * Returns an array with the files and directories in the current directory.
238
	 *
239
	 * @param bool $recursive = false
240
	 * @param bool $showHidden = false
241
	 * @return string[] an array with the files and directories in the current directory.
242
	 */
243 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...
244
	{
245
		$result = [];
246
247 1
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
248
			$result[] = $element->getFilename();
249 1
		}
250
251 1
		return $result;
252 1
	}
253 1
254
	/**
255
	 * Returns an array with the directories in the current directory.
256
	 *
257 1
	 * @param bool $recursive = false
258
	 * @param bool $showHidden = false
259
	 * @return string[] an array with the directories in the current directory.
260
	 */
261 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...
262
	{
263
		$result = [];
264
265
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
266 1
			if ($element->isDir()) {
267
				$result[] = $element->getFilename();
268 1
			}
269 1
		}
270
271
		return $result;
272 1
	}
273
274
	/**
275
	 * Returns an array with the files in the current directory.
276
	 *
277
	 * @param bool $recursive = false
278
	 * @param bool $showHidden = false
279
	 * @return string[] an array with the files in the current directory.
280
	 */
281 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...
282 1
	{
283
		$result = [];
284 1
285 1
		foreach ($this->listAllIterator($recursive, $showHidden) as $element) {
286
			if ($element->isFile()) {
287
				$result[] = $element->getFilename();
288 1
			}
289 1
		}
290 1
291
		return $result;
292 1
	}
293
294
	/**
295
	 * Returns true if the file has been created.
296
	 *
297
	 * @param bool $override = false
298
	 * @return bool true if the file has been created.
299
	 */
300
	public function makeFile($override = false)
301
	{
302 2
		if ($this->exists() && !$override) {
303
			return false;
304 2
		}
305 2
306
		return file_put_contents($this->path, '') !== false;
307
	}
308 2
309
	/**
310 2
	 * Returns true if the directory has been created.
311 1
	 *
312
	 * @param bool $recursive = false
313
	 * @param int $permissions = 0755
314 2
	 * @return bool true if the directory has been created.
315
	 */
316 2
	public function makeDirectory($recursive = false, $permissions = 0775)
317
	{
318
		if ($this->exists()) {
319
			return false;
320
		}
321
322
		$old = umask(0777 - $permissions);
323
		$result = mkdir($this->path, $permissions, $recursive);
324
		umask($old);
325
326 1
		return $result;
327
	}
328 1
329
	/**
330
	 * Returns true if the file is succesfully moved.
331
	 *
332
	 * @param string $path
333
	 * @param bool $override = false
334
	 * @return bool true if the file is succesfully moved.
335
	 */
336
	public function move($path, $override = false)
337 1
	{
338
		if (!$this->exists()) {
339 1
			return false;
340 1
		}
341
342
		$file = new File($path);
343 1
344 1
		if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) {
345
			return false;
346
		}
347 1
348
		$this->path = $file->getPath();
349
350
		return true;
351
	}
352
353
	/**
354
	 * Returns true if the file is succesfully renamed.
355 1
	 *
356
	 * @param string $file
357 1
	 * @param bool $override = false
358 1
	 * @return bool true if the file is succesfully renamed.
359
	 */
360
	public function rename($file, $override = false)
361 1
	{
362
		return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override);
363
	}
364
365
	/**
366
	 * Returns true if the directory is succesfully removed.
367
	 *
368
	 * @param bool $recursive = false
369
	 * @return bool true if the directory is succesfully removed.
370 7
	 */
371
	public function removeDirectory($recursive = false)
372 7
	{
373
		if (!$recursive) {
374 7
			return rmdir($this->path);
375 2
		}
376
377
		foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) {
378 5
			$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
379
		}
380
381
		return true;
382
	}
383
384
	/**
385
	 * Returns true if the file is succesfully removed.
386
	 *
387
	 * @return bool true if the file is succesfully removed.
388 3
	 */
389
	public function removeFile()
390 3
	{
391 1
		if (!$this->isFile()) {
392
			return false;
393 2
		}
394
395
		return unlink($this->path);
396
	}
397
398
	/**
399
	 * Returns the content of the file.
400
	 *
401
	 * @return string the content of the file.
402 3
	 * @throws FileException on failure.
403
	 */
404 3
	public function read()
405 1
	{
406
		$result = file_get_contents($this->path);
407 2
408
		if ($result === false) {
409
			throw new FileException('Can\'t read the content.');
410
		}
411
412
		return $result;
413
	}
414
415
	/**
416
	 * Append the given content.
417
	 *
418
	 * @param string $content
419
	 * @return null
420
	 * @throws FileException on failure.
421
	 */
422
	public function append($content)
423
	{
424
		if (file_put_contents($this->path, $content, \FILE_APPEND) === false) {
425
			throw new FileException('Can\'t append the given content.');
426
		}
427
	}
428
429
	/**
430
	 * Write the given content.
431
	 *
432
	 * @param string $content
433
	 * @return null
434
	 * @throws FileException on failure.
435
	 */
436
	public function write($content)
437
	{
438
		if (file_put_contents($this->path, $content) === false) {
439
			throw new FileException('Can\'t write the given content.');
440
		}
441
	}
442
}
443