Completed
Pull Request — master (#2)
by James Ekow Abaka
01:17
created

Directory::directoryOperation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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