Completed
Push — dev ( 0c6e86...8d8070 )
by James Ekow Abaka
01:21
created

Filesystem::checkExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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\filesystem\File;
30
31
/**
32
 * A collection of filesystem utilities.
33
 * 
34
 */
35
class Filesystem
36
{
37
    /**
38
     * Checks if a file is writeable.
39
     * In cases where the file cannot be written to an exception is thrown.
40
     *
41
     * @param string $path The path to the file to be checked.
42
     * @throws exceptions\FileNotWriteableException
43
     */
44 4
    public static function checkWritable(string $path) : void
45
    {
46 4
        if (!is_writable($path)) {
47 2
            throw new exceptions\FileNotWriteableException("File $path is not writeable");
48
        }
49 4
    }
50
51
    /**
52
     * Check if a file is readable.
53
     * In cases where the file cannot be read, an exception is thrown.
54
     *
55
     * @param string $path The path to the file to be checked.
56
     * @throws exceptions\FileNotReadableException
57
     */
58 5
    public static function checkReadable(string $path) : void
59
    {
60 5
        if (!is_readable($path)) {
61
            throw new exceptions\FileNotReadableException("File $path is not readable");
62
        }
63 5
    }
64
65
    /**
66
     * @param $path
67
     * @throws exceptions\FileNotFoundException
68
     */
69 7
    public static function checkExists(string $path) : void
70
    {
71 7
        if (!file_exists($path)) {
72 4
            throw new exceptions\FileNotFoundException($path);
73
        }
74 7
    }
75
76
    /**
77
     * @param string $path
78
     * @throws exceptions\FileAlreadyExistsException
79
     */
80 2
    public static function checkNotExists(string $path) : void
81
    {
82 2
        if(file_exists($path)) {
83
            throw new exceptions\FileAlreadyExistsException($path);
84
        }
85 2
    }
86
87
    /**
88
     * @param $path
89
     * @throws exceptions\FileNotFoundException
90
     * @throws exceptions\FileNotWriteableException
91
     */
92 2
    public static function checkWriteSafety($path)
93
    {
94 2
        Filesystem::checkExists($path);
95 2
        Filesystem::checkWritable($path);
96 2
    }
97
98
    /**
99
     * @param $path
100
     * @throws exceptions\FileNotFoundException
101
     * @throws exceptions\FileNotReadableException
102
     */
103
    public static function checkReadSafety($path)
104
    {
105
        Filesystem::checkExists($path);
106
        Filesystem::checkReadable($path);
107
    }
108
109 5
    public static function get($path)
110
    {
111 5
        if (is_dir($path)) {
112 5
            return new filesystem\Directory($path);
113
        }
114 5
        return new filesystem\File($path);
115
    }
116
}
117