Completed
Pull Request — master (#9)
by James Ekow Abaka
01:16
created

Filesystem::checkExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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