Test Failed
Push — master ( a1b0fe...95c33d )
by Petr
10:27
created

Volume::increment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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