Completed
Push — dev ( 4b701d...397072 )
by James Ekow Abaka
03:37
created

Filesystem::checkNotExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
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
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 11
     * @param string $path The path to the file to be checked.
44
     * @throws exceptions\FileNotWriteableException
45 11
     */
46 2
    public static function checkWritable(string $path) : void
47
    {
48 9
        if (!is_writable($path)) {
49
            throw new exceptions\FileNotWriteableException("Location $path is not writeable");
50
        }
51
    }
52
53
    /**
54
     * Check if a file is readable.
55
     * In cases where the file cannot be read, an exception is thrown.
56
     *
57 11
     * @param string $path The path to the file to be checked.
58
     * @throws exceptions\FileNotReadableException
59 11
     */
60 2
    public static function checkReadable(string $path) : void
61
    {
62 9
        if (!is_readable($path)) {
63
            throw new exceptions\FileNotReadableException("Location $path is not readable");
64
        }
65
    }
66
67
    /**
68
     * Checks if a file exists and throws an exception if not.
69
     *
70 14
     * @param $path
71
     * @throws exceptions\FileNotFoundException
72 14
     */
73 5
    public static function checkExists(string $path) : void
74
    {
75 11
        if (!file_exists($path)) {
76
            throw new exceptions\FileNotFoundException("Location '$path' does not exist");
77
        }
78
    }
79
80
    /**
81
     * Checks if a file exists and throws an exception if it does.
82
     *
83 3
     * @param string $path
84
     * @throws exceptions\FileAlreadyExistsException
85 3
     */
86 1
    public static function checkNotExists(string $path) : void
87
    {
88 2
        if(file_exists($path)) {
89
            throw new exceptions\FileAlreadyExistsException("Location '$path' already exists");
90
        }
91
    }
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 7
     * @throws exceptions\FileNotFoundException
98
     * @throws exceptions\FileNotWriteableException
99 7
     */
100 6
    public static function checkWriteSafety($path)
101 5
    {
102
        Filesystem::checkExists($path);
103
        Filesystem::checkWritable($path);
104
    }
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 3
     * @throws exceptions\FileNotFoundException
111
     * @throws exceptions\FileNotReadableException
112 3
     */
113 2
    public static function checkReadSafety($path)
114 1
    {
115
        Filesystem::checkExists($path);
116
        Filesystem::checkReadable($path);
117
    }
118
119
    /**
120
     * Return an instance of the relevant FileInterface (File or Directory) for a file in a given path.
121
     *
122 6
     * @param string $path
123
     * @return FileInterface
124 6
     */
125 6
    public static function get($path)
126
    {
127 6
        if (is_dir($path)) {
128
            return new filesystem\Directory($path);
129
        } else if(is_file($path)) {
130
            return new filesystem\File($path);
131
        }
132
        throw new FileNotFoundException("Could not get location '{$path}'");
133
    }
134
135
    public static function file($path)
136
    {
137
        return new filesystem\File($path);
138
    }
139
140
    public static function directory($path)
141
    {
142
        return new filesystem\Directory($path);
143
    }
144
145
    public static function glob($glob)
146
    {
147
        return new filesystem\FileCollection(glob($glob));
148
    }
149
}
150