Completed
Push — dev ( 7d7f55...0c6e86 )
by James Ekow Abaka
01:53
created

Directory::getContents()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
crap 4
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\Filesystem;
6
use ntentan\utils\exceptions\FilesystemException;
7
8
/**
9
 * A directory on the filesystem.
10
 *
11
 * @package ntentan\utils\filesystem
12
 */
13
class Directory implements FileInterface
14
{
15
16
    /**
17
     * Full path to the directory.
18
     *
19
     * @var string
20
     */
21
    private $path;
22
23
    /**
24
     * Create a new instance with a path.
25
     *
26
     * @param string $path Optional path pointed to by new instance. Path does not have to exist.
27
     */
28 1
    public function __construct(string $path = null)
29
    {
30 1
        $this->path = $path;
31 1
    }
32
33
    /**
34
     * Recursively copies directory and its contents to destination.
35
     *
36
     * @param string $destination
37
     * @throws FilesystemException
38
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
39
     */
40
    public function copyTo(string $destination): void
41
    {
42
        Filesystem::checkWritable($destination);
43
        if (!file_exists($destination)) {
44
            self::create($destination);
45
        }
46
47
        $files = glob("$this->path/*");
48
        foreach ($files as $file) {
49
            $newFile = "$destination/" . basename("$file");
50
            if (is_dir($file)) {
51
                self::create($newFile);
52
                (new Directory($file))->copyTo($newFile);
53
            } else {
54
                copy($file, $newFile);
55
            }
56
        }
57
    }
58
59
    /**
60
     * Get the size of all contents in the directory.
61
     *
62
     * @return integer
63
     */
64
    public function getSize() : integer
65
    {
66
67
    }
68
69
    public function moveTo(string $destination) : void
70
    {
71
72
    }
73
74
    public static function create($path, $permissions = 0755)
75
    {
76
        if (file_exists($path) && !is_dir($path)) {
77
            throw new FilesystemException("A file already exists in the location of [$path]");
78
        }
79
        if (!file_exists($path)) {
80
            mkdir($path, $permissions, true);
81
        }
82
        return new Directory($path);
83
    }
84
85
    public function delete() : void
86
    {
87
88
    }
89
90
    public function getPath() : string
91
    {
92
        return $this->path;
93
    }
94
95
    /**
96
     * @throws FilesystemException
97
     * @throws \ntentan\utils\exceptions\FileNotFoundException
98
     * @throws \ntentan\utils\exceptions\FileNotReadableException
99
     */
100 1
    public function getContents()
101
    {
102 1
        Filesystem::checkExists($this->path);
103 1
        Filesystem::checkReadable($this->path);
104 1
        $contents = [];
105
106 1
        $files = scandir($this->path);
107 1
        foreach ($files as $file) {
108 1
            if($file != '.' && $file != '..') {
109 1
                $contents[] = Filesystem::get("$this->path/$file");
110
            }
111
        }
112 1
        return $contents;
113
    }
114
115 1
    public function __toString()
116
    {
117 1
        return $this->path;
118
    }
119
}
120