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

Filesystem   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 48.39%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 74
ccs 15
cts 31
cp 0.4839
rs 10
c 0
b 0
f 0

7 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 checkReadSafety() 0 5 1
A createDirectoryStructure() 0 11 3
A get() 0 7 2
1
<?php
2
/*
3
 * Ntentan Framework
4
 * Copyright (c) 2008-2015 James Ekow Abaka Ainooson
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 * 
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 * 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
24
 * 
25
 */
26
27
namespace ntentan\utils;
28
29
use ntentan\utils\filesystem\Directory;
30
use ntentan\utils\filesystem\File;
31
32
/**
33
 * A collection of filesystem utilities.
34
 * 
35
 */
36
class Filesystem
37
{
38
    /**
39
     * Checks if a file is writeable.
40
     * In cases where the file cannot be written to an exception is thrown.
41
     *
42
     * @param string $path The path to the file to be checked.
43
     * @throws exceptions\FileNotWriteableException
44
     * @return bool
45
     */
46 2
    public static function checkWritable($path) : bool
47
    {
48 2
        if (!is_writable($path)) {
49 2
            throw new exceptions\FileNotWriteableException("File $path is not writeable");
50
        }
51 2
        return true;
52
    }
53
54
    /**
55
     * Check if a file is readable.
56
     * In cases where the file cannot be read, an exception is thrown.
57
     *
58
     * @param string $path The path to the file to be checked.
59
     * @throws exceptions\FileNotReadableException
60
     * @return bool
61
     */
62 1
    public static function checkReadable($path) : bool
63
    {
64 1
        if (!is_readable($path)) {
65
            throw new exceptions\FileNotReadableException("File $path is not readable");
66
        }
67 1
        return true;
68
    }
69
70 3
    public static function checkExists($path)
71
    {
72 3
        if (!file_exists($path)) {
73 2
            throw new exceptions\FileNotFoundException($path);
74
        }
75 3
        return true;
76
    }
77
78
    public static function checkWriteSafety($path)
79
    {
80
        Filesystem::checkExists($path);
81
        Filesystem::checkWritable($path);
82
    }
83
84
    public static function checkReadSafety($path)
85
    {
86
        Filesystem::checkExists($path);
87
        Filesystem::checkReadable($path);
88
    }
89
90
    public static function createDirectoryStructure($structure, $basePath)
91
    {
92
        foreach ($structure as $key => $value) {
93
            if (is_numeric($key)) {
94
                Directory::create("$basePath/$value");
95
            } else {
96
                Directory::create("$basePath/$key");
97
                self::createDirectoryStructure($value, "$basePath/$key");
98
            }
99
        }
100
    }
101
102 1
    public static function get($path)
103
    {
104 1
        if (is_dir($path)) {
105 1
            return new filesystem\Directory($path);
106
        }
107 1
        return new filesystem\File($path);
108
    }
109
}
110