Completed
Push — master ( e51964...81b8f9 )
by James Ekow Abaka
01:59
created

Filesystem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 29.63%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 0
cbo 4
dl 0
loc 53
ccs 8
cts 27
cp 0.2963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkWritable() 0 7 2
A checkReadable() 0 7 2
A checkExists() 0 7 2
A checkWriteSafety() 0 5 1
A createDirectoryStructure() 0 11 3
A get() 0 8 2
1
<?php
2
namespace ntentan\utils;
3
4
use ntentan\utils\filesystem\Directory;
5
use ntentan\utils\filesystem\File;
6
7
class Filesystem
8
{   
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
9 2
    public static function checkWritable($path)
10
    {
11 2
        if(!is_writable($path)) {
12 2
            throw new exceptions\FileNotWriteableException("File $path is not writeable");
13
        }
14 2
        return true;
15
    }
16
    
17
    public static function checkReadable($path)
18
    {
19
        if(!is_readable($path)) {
20
            throw new exceptions\FileNotReadableException("File $path is not readable");
21
        }
22
        return true;
23
    }
24
    
25 2
    public static function checkExists($path)
26
    {
27 2
        if(!file_exists($path)) {
28 2
            throw new exceptions\FileNotFoundException("File $path does not exist");
29
        }
30 2
        return true;
31
    }
32
    
33
    public static function checkWriteSafety($path)
34
    {
35
        Filesystem::checkExists($path);
36
        Filesystem::checkWritable($path);        
37
    }
38
    
39
    public static function createDirectoryStructure($structure, $basePath)
40
    {
41
        foreach($structure as $key => $value) {
42
            if(is_numeric($key)) {
43
                Directory::create("$basePath/$value");
44
            } else {
45
                Directory::create("$basePath/$key");
46
                self::createDirectoryStructure($value, "$basePath/$key");
47
            }
48
        }
49
    }
50
    
51
    public static function get($path)
52
    {
53
        if(is_dir($path))
54
        {
55
            return new filesystem\Directory($path);
56
        }
57
        return new filesystem\File($path);
58
    }
59
}
60