Completed
Push — dev ( f5f53b...0289d6 )
by James Ekow Abaka
01:24 queued 11s
created

Filesystem   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 67.44%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 7
dl 0
loc 158
ccs 29
cts 43
cp 0.6744
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A checkWritable() 0 6 2
A checkReadable() 0 6 2
A checkExists() 0 6 2
A checkNotExists() 0 6 2
A checkWriteSafety() 0 5 1
A checkReadSafety() 0 5 1
A get() 0 9 3
A file() 0 4 1
A directory() 0 4 1
A glob() 0 4 1
B getAbsolutePath() 0 10 7
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\exceptions\FileNotFoundException;
30
use ntentan\utils\filesystem\Directory;
31
use ntentan\utils\filesystem\File;
32
use ntentan\utils\filesystem\FileInterface;
33
34
/**
35
 * A collection of filesystem utilities.
36
 *
37
 */
38
class Filesystem
39
{
40
    /**
41
     * Checks if a file is writeable.
42
     * In cases where the file cannot be written to an exception is thrown.
43
     *
44
     * @param string $path The path to the file to be checked.
45
     * @param string|null $message
46
     * @throws exceptions\FileNotWriteableException
47
     */
48 13
    public static function checkWritable(string $path, string $message = null): void
49
    {
50 13
        if (!is_writable($path)) {
51 2
            throw new exceptions\FileNotWriteableException($message ?? "Location [$path] is not writeable");
52
        }
53 11
    }
54
55
    /**
56
     * Check if a file is readable.
57
     * In cases where the file cannot be read, an exception is thrown.
58
     *
59
     * @param string $path The path to the file to be checked.
60
     * @param string|null $message
61
     * @throws exceptions\FileNotReadableException
62
     */
63 9
    public static function checkReadable(string $path, string $message = null): void
64
    {
65 9
        if (!is_readable($path)) {
66 1
            throw new exceptions\FileNotReadableException($message ?? "Location $path is not readable");
67
        }
68 8
    }
69
70
    /**
71
     * Checks if a file exists and throws an exception if not.
72
     *
73
     * @param string $path
74
     * @param string|null $message
75
     * @throws FileNotFoundException
76
     */
77 13
    public static function checkExists(string $path, string $message = null): void
78
    {
79 13
        if (!file_exists($path)) {
80 2
            throw new exceptions\FileNotFoundException($message ?? "Location '$path' does not exist");
81
        }
82 11
    }
83
84
    /**
85
     * Checks if a file exists and throws an exception if it does.
86
     *
87
     * @param string $path
88
     * @param string|null $message
89
     * @throws exceptions\FileAlreadyExistsException
90
     */
91 3
    public static function checkNotExists(string $path, string $message = null): void
92
    {
93 3
        if (file_exists($path)) {
94 1
            throw new exceptions\FileAlreadyExistsException($message ?? "Location '$path' already exists");
95
        }
96 2
    }
97
98
    /**
99
     * Checks if a file exists and is writeable and throws a relevant exception if either condition is not met.
100
     *
101
     * @param string $path
102
     * @param string|null $message
103
     * @throws FileNotFoundException
104
     * @throws exceptions\FileNotWriteableException
105
     */
106 9
    public static function checkWriteSafety(string $path, string $message = null) : void
107
    {
108 9
        Filesystem::checkExists($path, $message);
109 8
        Filesystem::checkWritable($path, $message);
110 7
    }
111
112
    /**
113
     * Checks if a file exists and is readable and throws a relevant excetion if either condition is not met.
114
     *
115
     * @param string $path
116
     * @param string $message
117
     * @throws FileNotFoundException
118
     * @throws exceptions\FileNotReadableException
119
     */
120
    public static function checkReadSafety(string $path, string $message) : void
121
    {
122
        Filesystem::checkExists($path, $message);
123
        Filesystem::checkReadable($path, $message);
124
    }
125
126
    /**
127
     * Return an instance of the relevant FileInterface (File or Directory) for a file in a given path.
128
     *
129
     * @param string $path
130
     * @return FileInterface
131
     * @throws FileNotFoundException
132
     */
133 1
    public static function get($path) : FileInterface
134
    {
135 1
        if (is_dir($path)) {
136 1
            return new filesystem\Directory($path);
137 1
        } else if (is_file($path)) {
138 1
            return new filesystem\File($path);
139
        }
140
        throw new FileNotFoundException("Could not get location '{$path}'");
141
    }
142
143
    /**
144
     * Return an instance of the File object for the given path.
145
     *
146
     * @param $path
147
     * @return File
148
     */
149 5
    public static function file($path) : File
150
    {
151 5
        return new filesystem\File($path);
152
    }
153
154
    /**
155
     * Return an instance of the Directory object for the given path.
156
     *
157
     * @param $path
158
     * @return Directory
159
     */
160 5
    public static function directory($path) : Directory
161
    {
162 5
        return new filesystem\Directory($path);
163
    }
164
165
    /**
166
     * Returns a file collection for all whose name match with the provided pattern.
167
     * The format for the pattern is similar to those used by most shells as wildcards for selecting files.
168
     *
169
     * @param $pattern
170
     * @return filesystem\FileCollection
171
     */
172
    public static function glob($pattern)
173
    {
174
        return new filesystem\FileCollection(glob($pattern));
175
    }
176
177
    /**
178
     * Takes any path (relative or absolute) and returns its absolute form relative to a given path. When a relative
179
     * path is not provided, the current working directory is used.
180
     *
181
     * @param $path
182
     * @param null $relativeTo
183
     * @return string
184
     */
185
    public static function getAbsolutePath($path, $relativeTo = null)
186
    {
187
        $relativeTo = $relativeTo ?? getcwd();
188
        if (preg_match('/^(\\\\|\/)?\.|\.\.\\\\\//', $path) == 1 || (preg_match('/^[a-zA-Z]:/', $path) == 0 && PHP_OS == "Windows")) {
189
            $path = $relativeTo . DIRECTORY_SEPARATOR . $path;
190
        } else if (isset($path[0]) && $path[0] !== '\\' && $path[0] !== '/') {
191
            $path = $relativeTo . DIRECTORY_SEPARATOR . $path;
192
        }
193
        return $path;
194
    }
195
}
196