Directory   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 95
ccs 27
cts 27
cp 1
rs 10
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 2 1
A __toString() 0 2 1
A rewind() 0 2 1
A next() 0 2 1
A key() 0 2 1
A current() 0 2 1
A getIterator() 0 6 2
A __construct() 0 2 1
A delete() 0 9 4
A make() 0 3 3
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\file;
11
12
use DirectoryIterator;
13
use Iterator;
14
use phootwork\file\exception\FileException;
15
use Stringable;
16
17
/**
18
 * Class Directory
19
 */
20
class Directory implements Iterator, Stringable {
21
	use FileOperationTrait;
22
23
	/** @var ?DirectoryIterator */
24
	private ?DirectoryIterator $iterator = null;
25
26 20
	public function __construct(string|Stringable $filename) {
27 20
		$this->pathname = (string) $filename;
28
	}
29
30
	/**
31
	 * Creates the directory
32
	 * 
33
	 * @param int $mode
34
	 *
35
	 * @throws FileException when something goes wrong
36
	 */
37 15
	public function make(int $mode = 0777): void {
38 15
		if (!$this->exists() && !@mkdir($this->pathname, $mode, true)) {
39 1
			throw new FileException(sprintf('Failed to create directory "%s"', $this->pathname));
40
		}
41
	}
42
43
	/**
44
	 * Recursively deletes the directory
45
	 *
46
	 * @throws FileException when something goes wrong
47
	 */
48 5
	public function delete(): void {
49 5
		foreach ($this as $file) {
50 5
			if (!$file->isDot()) {
51 5
				$file->delete();
52
			}
53
		}
54
55 5
		if (!@rmdir($this->pathname)) {
56 1
			throw new FileException(sprintf('Failed to delete directory "%s"', $this->pathname));
57
		}
58
	}
59
60
	/**
61
	 * Returns a directory iterator
62
	 * 
63
	 * @return DirectoryIterator
64
	 */
65 6
	private function getIterator(): DirectoryIterator {
66 6
		if ($this->iterator === null) {
67 6
			$this->iterator = new DirectoryIterator($this->pathname);
68
		}
69
70 6
		return $this->iterator;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->iterator could return the type null which is incompatible with the type-hinted return DirectoryIterator. Consider adding an additional type-check to rule them out.
Loading history...
71
	}
72
73
	/**
74
	 * @return FileDescriptor
75
	 *
76
	 * @internal
77
	 */
78 6
	public function current(): FileDescriptor {
79 6
		return FileDescriptor::fromFileInfo($this->getIterator()->current());
80
	}
81
82
	/**
83
	 * @internal
84
	 */
85 1
	public function key(): float|bool|int|string|null {
86 1
		return $this->getIterator()->key();
87
	}
88
89
	/**
90
	 * @internal
91
	 */
92 6
	public function next(): void {
93 6
		$this->getIterator()->next();
94
	}
95
96
	/**
97
	 * @internal
98
	 */
99 6
	public function rewind(): void {
100 6
		$this->getIterator()->rewind();
101
	}
102
103
	/**
104
	 * @internal
105
	 */
106 6
	public function valid(): bool {
107 6
		return $this->getIterator()->valid();
108
	}
109
110
	/**
111
	 * String representation of this directory as pathname
112
	 */
113 2
	public function __toString(): string {
114 2
		return $this->pathname;
115
	}
116
}
117