Completed
Push — dev ( f5d068...9b088b )
by James Ekow Abaka
01:30
created

Directory   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.57%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 160
ccs 48
cts 53
cp 0.9057
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A copyTo() 0 5 1
A getSize() 0 4 1
A moveTo() 0 7 1
A create() 0 20 5
A createIfNotExists() 0 9 2
A delete() 0 5 1
A getPath() 0 4 1
B getFiles() 0 17 7
A __toString() 0 4 1
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\exceptions\FileAlreadyExistsException;
6
use ntentan\utils\exceptions\FileNotFoundException;
7
use ntentan\utils\exceptions\FileNotReadableException;
8
use ntentan\utils\exceptions\FileNotWriteableException;
9
use ntentan\utils\Filesystem;
10
use ntentan\utils\exceptions\FilesystemException;
11
12
/**
13
 * Represents a directory from the filesystem.
14
 *
15
 * @package ntentan\utils\filesystem
16
 * 
17
 */
18
class Directory implements FileInterface
19
{
20
21
    /**
22
     * Full path to the directory.
23
     *
24
     * @var string
25
     */
26
    private $path;
27
28
    /**
29
     * Create a new instance with a path.
30
     *
31
     * @param string $path Optional path pointed to by new instance. Path does not have to exist.
32
     */
33 6
    public function __construct(string $path = null)
34
    {
35 6
        $this->path = $path;
36 6
    }
37
    
38
    /**
39
     * Recursively copies directory and its contents to destination.
40
     *
41
     * @param string $destination
42
     * @throws FileNotFoundException
43
     * @throws FilesystemException
44
     * @throws FileAlreadyExistsException
45
     * @throws FileNotReadableException
46
     * @throws FileNotWriteableException
47
     */
48 1
    public function copyTo(string $destination, string $overwite = self::OVERWRITE_ALL): void
49
    {
50 1
        Filesystem::directory($destination)->create(true);
51 1
        $this->getFiles()->copyTo($destination, $overwite);
52 1
    }
53
54
    /**
55
     * Recursively get the size of all contents in the directory.
56
     *
57
     * @return integer
58
     * @throws FileNotFoundException
59
     * @throws FilesystemException
60
     * @throws FileNotReadableException
61
     */
62 1
    public function getSize() : int
63
    {
64 1
        return $this->getFiles()->getSize();
65
    }
66
67
    /**
68
     * Recursively move a directory and its contents to another location.
69
     *
70
     * @param string $destination
71
     * @throws FileNotFoundException
72
     * @throws FilesystemException
73
     * @throws FileAlreadyExistsException
74
     * @throws FileNotReadableException
75
     * @throws FileNotWriteableException
76
     */
77 1
    public function moveTo(string $destination, string $overwite = self::OVERWRITE_ALL) : void
78
    {
79 1
        Filesystem::directory($destination)->create(true);
80 1
        $this->getFiles()->moveTo($destination, $overwite);
81 1
        $this->delete();
82 1
        $this->path = $destination;
83 1
    }
84
85
    /**
86
     * Create the directory pointed to by path.
87
     *
88
     * @param int $permissions
89
     * @return Directory
90
     * @throws FileAlreadyExistsException
91
     * @throws FileNotWriteableException
92
     */
93 2
    public function create($recursive=false, $permissions = 0755)
94
    {
95 2
        Filesystem::checkNotExists($this->path);
96 2
        $parent = $this->path;
97 2
        if($recursive) {
98 2
            $segments = explode(DIRECTORY_SEPARATOR, $this->path);
99 2
            $parent = "";
100 2
            foreach($segments as $segment) {
101 2
                $parent .= $segment . DIRECTORY_SEPARATOR;
102 2
                if(!is_dir($parent)) {
103 2
                    break;
104
                }
105
            }
106
        }
107 2
        $parent = dirname($parent);
108 2
        Filesystem::checkWritable($parent == "" ? '.' : $parent);
109 2
        mkdir($this->path, $permissions, true);
110
111 2
        return $this;
112
    }
113
114
    public function createIfNotExists($recursive=false, $permissions = 0755)
115
    {
116
        try {
117
            $this->create($recursive, $permissions);
118
        } catch (FileAlreadyExistsException $exception) {
119
            // Do nothing
120
        }
121
        return $this;
122
    }
123
124
    /**
125
     * Recursively delete the directory and all its contents.
126
     *
127
     * @throws FileNotFoundException
128
     * @throws FilesystemException
129
     * @throws FileNotReadableException
130
     */
131 2
    public function delete() : void
132
    {
133 2
        $this->getFiles()->delete();
134 2
        rmdir($this->path);
135 2
    }
136
137
    /**
138
     * Get the path of the directory.
139
     *
140
     * @return string
141
     */
142 1
    public function getPath() : string
143
    {
144 1
        return $this->path;
145
    }
146
147
    /**
148
     * Get the files in the directory.
149
     *
150
     * @throws FilesystemException
151
     * @throws FileNotFoundException
152
     * @throws FileNotReadableException
153
     * @return FileCollection | array<string>
154
     */
155 5
    public function getFiles($recursive=false, $returnStrings=false)
156
    {
157 5
        Filesystem::checkExists($this->path);
158 5
        Filesystem::checkReadable($this->path);
159 5
        $contents = [];
160
161 5
        $files = scandir($this->path);
162 5
        foreach ($files as $file) {
163 5
            if($file == '.' || $file == '..') continue;
164 5
            $path = "$this->path/$file";
165 5
            if(is_dir($path) && $recursive) {
166
                $contents = array_merge($contents, Filesystem::directory($path)->getFiles(true, true));
167
            }
168 5
            $contents[] = $path;
169
        }
170 5
        return $returnStrings ? $contents : new FileCollection($contents);
171
    }
172
173 3
    public function __toString()
174
    {
175 3
        return $this->path;
176
    }
177
}
178