Completed
Push — master ( f1cb99...3ef691 )
by WEBEWEB
02:22
created

DirectoryHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 2
A delete() 0 6 2
A isEmpty() 0 6 2
A rename() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\FileSystem;
13
14
use WBW\Library\Core\Exception\FileSystem\FileNotFoundException;
15
16
/**
17
 * Directory helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\IO
21
 */
22
class DirectoryHelper {
23
24
    /**
25
     * Create a directory.
26
     *
27
     * @param string $dirname The directory name.
28
     * @param int $mode The directory mode.
29
     * @param boolean $recursive Recursive ?
30
     * @return boolean Returns true in case of success, false otherwise or null if the directory exists.
31
     */
32
    public static function create($dirname, $mode = 0755, $recursive = false) {
33
        if (true === file_exists($dirname)) {
34
            return null;
35
        }
36
        return mkdir($dirname, $mode, $recursive);
37
    }
38
39
    /**
40
     * Delete a directory.
41
     *
42
     * @param string $dirname The directory name.
43
     * @return boolean Returns true in case of success, false otherwise or null if the directory can't be deleted.
44
     */
45
    public static function delete($dirname) {
46
        if (true !== self::isEmpty($dirname)) {
47
            return null;
48
        }
49
        return rmdir($dirname);
50
    }
51
52
    /**
53
     * Determines if a directory is empty.
54
     *
55
     * @param string $dirname The directory name.
56
     * @return boolean Returns true in case of success, false otherwise or null if the directory is not readable.
57
     */
58
    public static function isEmpty($dirname) {
59
        if (false === is_readable($dirname)) {
60
            return null;
61
        }
62
        return (count(scandir($dirname)) == 2);
63
    }
64
65
    /**
66
     * Rename a directory.
67
     *
68
     * @param string $oldDirname The old directory name.
69
     * @param string $newDirname The new directory name.
70
     * @return boolean Returns true in case of success, false otherwise or null if the new directory name already exists.
71
     * @throws FileNotFoundException Throws a file not found exception if the directory does not exists.
72
     * @see FileHelper
73
     */
74
    public static function rename($oldDirname, $newDirname) {
75
        return FileHelper::rename($oldDirname, $newDirname);
76
    }
77
78
}
79