Completed
Push — master ( 7942ad...1c8947 )
by WEBEWEB
01:50
created

DirectoryUtility   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
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 57
rs 10

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) 2017 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\Utility\IO;
13
14
use WBW\Library\Core\Exception\IO\FileNotFoundException;
15
16
/**
17
 * Directory utility.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Utility\IO
21
 * @final
22
 */
23
final class DirectoryUtility {
24
25
    /**
26
     * Create a directory.
27
     *
28
     * @param string $dirname The directory name.
29
     * @param integer $mode The directory mode.
30
     * @param boolean $recursive Recursive ?
31
     * @return boolean Returns true in case of success, false otherwise or null if the directory exists.
32
     */
33
    public static function create($dirname, $mode = 0755, $recursive = false) {
34
        if (true === file_exists($dirname)) {
35
            return null;
36
        }
37
        return mkdir($dirname, $mode, $recursive);
38
    }
39
40
    /**
41
     * Delete a directory.
42
     *
43
     * @param string $dirname The directory name.
44
     * @return boolean Returns true in case of success, false otherwise or null if the directory can't be deleted.
45
     */
46
    public static function delete($dirname) {
47
        if (true !== self::isEmpty($dirname)) {
48
            return null;
49
        }
50
        return rmdir($dirname);
51
    }
52
53
    /**
54
     * Determines if a directory is empty.
55
     *
56
     * @param string $dirname The directory name.
57
     * @return boolean Returns true in case of success, false otherwise or null if the directory is not readable.
58
     */
59
    public static function isEmpty($dirname) {
60
        if (false === is_readable($dirname)) {
61
            return null;
62
        }
63
        return (count(scandir($dirname)) == 2);
64
    }
65
66
    /**
67
     * Rename a directory.
68
     *
69
     * @param string $oldDirname The old directory name.
70
     * @param string $newDirname The new directory name.
71
     * @return boolean Returns true in case of success, false otherwise or null if the new directory name already exists.
72
     * @throws FileNotFoundException Throws a file not found exception if the directory does not exists.
73
     * @see FileUtility
74
     */
75
    public static function rename($oldDirname, $newDirname) {
76
        return FileUtility::rename($oldDirname, $newDirname);
77
    }
78
79
}
80