Passed
Push — master ( 309dcb...6acad1 )
by Petr
02:25
created

Volume::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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