Passed
Push — master ( 6acad1...7c550d )
by Petr
08:00
created

Volume   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 145
ccs 70
cts 70
cp 1
rs 9.6
wmc 35

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A exists() 0 3 1
A isWritable() 0 3 1
A load() 0 7 2
A created() 0 4 2
A isFile() 0 3 1
B lookup() 0 15 7
A size() 0 4 2
A save() 0 3 1
A isReadable() 0 3 1
A increment() 0 10 2
A isDir() 0 3 1
A remove() 0 3 1
A mkDir() 0 3 1
A check() 0 11 4
A decrement() 0 10 2
A copy() 0 3 1
A move() 0 5 2
A rmDir() 0 3 2
1
<?php
2
3
namespace kalanis\kw_storage\Storage\Target;
4
5
6
use kalanis\kw_storage\Extras\TRemoveCycle;
7
use kalanis\kw_storage\Extras\TVolumeCopy;
8
use kalanis\kw_storage\Interfaces\IStTranslations;
9
use kalanis\kw_storage\Interfaces\ITargetVolume;
10
use kalanis\kw_storage\StorageException;
11
use kalanis\kw_storage\Traits\TLang;
12
use Traversable;
13
14
15
/**
16
 * Class Volume
17
 * @package kalanis\kw_storage\Storage\Target
18
 * Store content onto volume
19
 */
20
class Volume implements ITargetVolume
21
{
22
    use TOperations;
23
    use TRemoveCycle;
24
    use TVolumeCopy;
25
    use TLang;
26
27 41
    public function __construct(?IStTranslations $lang = null)
28
    {
29 41
        $this->setStLang($lang);
30 41
    }
31
32 13
    public function check(string $key): bool
33
    {
34 13
        $sepPos = mb_strrpos($key, DIRECTORY_SEPARATOR);
35 13
        $path = (false === $sepPos) ? substr($key, 0) : substr($key, 0, intval($sepPos));
36 13
        if (!is_dir($path)) {
37 1
            if (file_exists($path)) {
38 1
                unlink($path);
39
            }
40 1
            return mkdir($path, 0777);
41
        }
42 12
        return true;
43
    }
44
45 16
    public function exists(string $key): bool
46
    {
47 16
        return @file_exists($key);
48
    }
49
50 9
    public function isDir(string $key): bool
51
    {
52 9
        return @is_dir($key);
53
    }
54
55 2
    public function isFile(string $key): bool
56
    {
57 2
        return @is_file($key);
58
    }
59
60 4
    public function isReadable(string $key): bool
61
    {
62 4
        return @is_readable($key);
63
    }
64
65 4
    public function isWritable(string $key): bool
66
    {
67 4
        return @is_writable($key);
68
    }
69
70 2
    public function mkDir(string $key, bool $recursive = false): bool
71
    {
72 2
        return @mkdir($key, 0777, $recursive);
73
    }
74
75 2
    public function rmDir(string $key, bool $recursive = false): bool
76
    {
77 2
        return $recursive ? $this->removeCycle($key) : @rmdir($key);
78
    }
79
80 10
    public function load(string $key)
81
    {
82 10
        $content = @file_get_contents($key);
83 10
        if (false === $content) {
84 5
            throw new StorageException($this->getStLang()->stCannotReadFile());
85
        }
86 8
        return $content;
87
    }
88
89 10
    public function save(string $key, $data, ?int $timeout = null): bool
90
    {
91 10
        return (false !== @file_put_contents($key, strval($data)));
92
    }
93
94 2
    public function copy(string $source, string $dest): bool
95
    {
96 2
        return $this->xcopy($source, $dest);
97
    }
98
99 2
    public function move(string $source, string $dest): bool
100
    {
101 2
        $v1 = $this->copy($source, $dest);
102 2
        $v2 = $this->removeCycle($source);
103 2
        return $v1 && $v2;
104
    }
105
106 12
    public function remove(string $key): bool
107
    {
108 12
        return @unlink($key);
109
    }
110
111 2
    public function size(string $key): ?int
112
    {
113 2
        $size = @filesize($key);
114 2
        return (false === $size) ? null : $size;
115
    }
116
117 2
    public function created(string $key): ?int
118
    {
119 2
        $created = @filemtime($key);
120 2
        return (false === $created) ? null : $created;
121
    }
122
123 2
    public function lookup(string $path): Traversable
124
    {
125 2
        $real = realpath($path);
126 2
        if (false === $real) {
127 2
            return;
128
        }
129 2
        $files = @scandir($real);
130 2
        if (!empty($files)) {
131 2
            foreach ($files as $file) {
132 2
                if (empty($file) || '.' == $file) {
133 2
                    yield '';
134 2
                } elseif ('..' == $file) {
135
                    // pass
136
                } else {
137 2
                    yield DIRECTORY_SEPARATOR . $file;
138
                }
139
            }
140
        }
141 2
    }
142
143 4
    public function increment(string $key, int $step = 1): bool
144
    {
145
        try {
146 4
            $number = intval($this->load($key)) + $step;
147 2
        } catch (StorageException $ex) {
148
            // no file
149 2
            $number = 1;
150
        }
151 4
        $this->remove($key); // hanging pointers
152 4
        return $this->save($key, $number);
153
    }
154
155 4
    public function decrement(string $key, int $step = 1): bool
156
    {
157
        try {
158 4
            $number = intval($this->load($key)) - $step;
159 2
        } catch (StorageException $ex) {
160
            // no file
161 2
            $number = 0;
162
        }
163 4
        $this->remove($key); // hanging pointers
164 4
        return $this->save($key, $number);
165
    }
166
}
167