Test Failed
Push — master ( 024905...9196d1 )
by Mikael
02:13
created

CCache::getStatusOfSubdir()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Deal with the cache directory and cached items.
4
 *
5
 */
6
class CCache
7
{
8
    /**
9
     * Path to the cache directory.
10
     */
11
    private $path;
12
13
14
15
    /**
16
     * Set the path to the cache dir which must exist.
17
     *
18
     * @param string path to the cache dir.
19
     *
20
     * @throws Exception when $path is not a directory.
21
     *
22
     * @return $this
23
     */
24
    public function setDir($path)
25
    {
26
        if (!is_dir($path)) {
27
            throw new Exception("Cachedir is not a directory.");
28
        }
29
30
        $this->path = $path;
31
32
        return $this;
33
    }
34
35
36
37
    /**
38
     * Get the path to the cache subdir and try to create it if its not there.
39
     *
40
     * @param string $subdir name of subdir
41
     * @param array  $create default is to try to create the subdir
42
     *
43
     * @return string | boolean as real path to the subdir or
44
     *                          false if it does not exists
45
     */
46
    public function getPathToSubdir($subdir, $create = true)
47
    {
48
        $path = realpath($this->path . "/" . $subdir);
49
50
        if (is_dir($path)) {
51
            return $path;
52
        }
53
54
        if ($create && is_writable($this->path)) {
55
            $path = $this->path . "/" . $subdir;
56
57
            if (mkdir($path)) {
58
                return realpath($path);
59
            }
60
        }
61
62
        return false;
63
    }
64
65
66
67
    /**
68
     * Get status of the cache subdir.
69
     *
70
     * @param string $subdir name of subdir
71
     *
72
     * @return string with status
73
     */
74
    public function getStatusOfSubdir($subdir)
75
    {
76
        $path = realpath($this->path . "/" . $subdir);
77
78
        $exists = is_dir($path);
79
        $res  = $exists ? "exists" : "does not exist";
80
        
81
        if ($exists) {
82
            $res .= is_writable($path) ? ", writable" : ", not writable";
83
        }
84
85
        return $res;
86
    }
87
88
89
90
    /**
91
     * Remove the cache subdir.
92
     *
93
     * @param string $subdir name of subdir
94
     *
95
     * @return null | boolean true if success else false, null if no operation
96
     */
97
    public function removeSubdir($subdir)
98
    {
99
        $path = realpath($this->path . "/" . $subdir);
100
101
        if (is_dir($path)) {
102
            return rmdir($path);
103
        }
104
105
        return null;
106
    }
107
}
108