Completed
Pull Request — master (#5)
by James Ekow Abaka
01:43
created

Filesystem::directory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
use ntentan\utils\exceptions\FileNotFoundException;
29
use ntentan\utils\filesystem\Directory;
30
use ntentan\utils\filesystem\File;
31
use ntentan\utils\filesystem\FileInterface;
32
33
/**
34
 * A collection of filesystem utilities.
35
 * 
36
 */
37
class Filesystem
38
{
39
    /**
40
     * Checks if a file is writeable.
41
     * In cases where the file cannot be written to an exception is thrown.
42
     *
43
     * @param string $path The path to the file to be checked.
44
     * @throws exceptions\FileNotWriteableException
45
     */
46 11
    public static function checkWritable(string $path) : void
47
    {
48 11
        if (!is_writable($path)) {
49 2
            throw new exceptions\FileNotWriteableException("Location [$path] is not writeable");
50
        }
51 9
    }
52
53
    /**
54
     * Check if a file is readable.
55
     * In cases where the file cannot be read, an exception is thrown.
56
     *
57
     * @param string $path The path to the file to be checked.
58
     * @throws exceptions\FileNotReadableException
59
     */
60 11
    public static function checkReadable(string $path) : void
61
    {
62 11
        if (!is_readable($path)) {
63 2
            throw new exceptions\FileNotReadableException("Location $path is not readable");
64
        }
65 9
    }
66
67
    /**
68
     * Checks if a file exists and throws an exception if not.
69
     *
70
     * @param $path
71
     * @throws exceptions\FileNotFoundException
72
     */
73 14
    public static function checkExists(string $path) : void
74
    {
75 14
        if (!file_exists($path)) {
76 5
            throw new exceptions\FileNotFoundException("Location '$path' does not exist");
77
        }
78 11
    }
79
80
    /**
81
     * Checks if a file exists and throws an exception if it does.
82
     *
83
     * @param string $path
84
     * @throws exceptions\FileAlreadyExistsException
85
     */
86 3
    public static function checkNotExists(string $path) : void
87
    {
88 3
        if(file_exists($path)) {
89 1
            throw new exceptions\FileAlreadyExistsException("Location '$path' already exists");
90
        }
91 2
    }
92
93
    /**
94
     * Checks if a file exists and is writeable and throws a relevant exception if either condition is not met.
95
     *
96
     * @param $path
97
     * @throws exceptions\FileNotFoundException
98
     * @throws exceptions\FileNotWriteableException
99
     */
100 7
    public static function checkWriteSafety($path)
101
    {
102 7
        Filesystem::checkExists($path);
103 6
        Filesystem::checkWritable($path);
104 5
    }
105
106
    /**
107
     * Checks if a file exists and is readable and throws a relevant excetion if either condition is not met.
108
     *
109
     * @param $path
110
     * @throws exceptions\FileNotFoundException
111
     * @throws exceptions\FileNotReadableException
112
     */
113 3
    public static function checkReadSafety($path)
114
    {
115 3
        Filesystem::checkExists($path);
116 2
        Filesystem::checkReadable($path);
117 1
    }
118
119
    /**
120
     * Return an instance of the relevant FileInterface (File or Directory) for a file in a given path.
121
     *
122
     * @param string $path
123
     * @return FileInterface
124
     * @throws FileNotFoundException
125
     */
126 1
    public static function get($path)
127
    {
128 1
        if (is_dir($path)) {
129 1
            return new filesystem\Directory($path);
130 1
        } else if(is_file($path)) {
131 1
            return new filesystem\File($path);
132
        }
133
        throw new FileNotFoundException("Could not get location '{$path}'");
134
    }
135
136 5
    public static function file($path)
137
    {
138 5
        return new filesystem\File($path);
139
    }
140
141 5
    public static function directory($path)
142
    {
143 5
        return new filesystem\Directory($path);
144
    }
145
146
    public static function glob($glob)
147
    {
148
        return new filesystem\FileCollection(glob($glob));
149
    }
150
}
151