Test Failed
Push — resize ( fbaf05...026e01 )
by Mikael
02:27
created

CCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
ccs 25
cts 28
cp 0.8929
wmc 13
lcom 1
cbo 1

4 Methods

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