Completed
Push — resize ( f2153e...0f3c1b )
by Mikael
04:20 queued 15s
created

CCache::getPathToSubdir()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.675
Metric Value
dl 0
loc 18
ccs 7
cts 10
cp 0.7
rs 8.8571
cc 5
eloc 9
nc 4
nop 2
crap 5.675
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 5
    public function setDir($path)
25
    {
26 5
        if (!is_dir($path)) {
27 1
            throw new Exception("Cachedir is not a directory.");
28
        }
29
30 4
        $this->path = $path;
31
32 4
        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 3
    public function getPathToSubdir($subdir, $create = true)
47
    {
48 3
        $path = realpath($this->path . "/" . $subdir);
49
50 3
        if (is_dir($path)) {
51
            return $path;
52
        }
53
54 3
        if ($create && is_writable($this->path)) {
55 3
            $path = $this->path . "/" . $subdir;
56
57 3
            if (mkdir($path)) {
58 3
                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 2
    public function getStatusOfSubdir($subdir)
75
    {
76 2
        $path = realpath($this->path . "/" . $subdir);
77
78 2
        $exists = is_dir($path);
79 2
        $res  = $exists ? "exists" : "does not exist";
80
        
81 2
        if ($exists) {
82 2
            $res .= is_writable($path) ? ", writable" : ", not writable";
83 2
        }
84
85 2
        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 3
    public function removeSubdir($subdir)
98
    {
99 3
        $path = realpath($this->path . "/" . $subdir);
100
101 3
        if (is_dir($path)) {
102 3
            return rmdir($path);
103
        }
104
105 1
        return null;
106
    }
107
}
108