Passed
Push — master ( 4570a8...dbcdaa )
by Petr
03:01
created

Volume::mkDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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 11
    public function check(string $key): bool
25
    {
26 11
        $sepPos = mb_strrpos($key, DIRECTORY_SEPARATOR);
27 11
        $path = (false === $sepPos) ? substr($key, 0) : substr($key, 0, intval($sepPos));
28 11
        if (!is_dir($path)) {
29 1
            if (file_exists($path)) {
30 1
                unlink($path);
31
            }
32 1
            return mkdir($path, 0777);
33
        }
34 10
        return true;
35
    }
36
37 15
    public function exists(string $key): bool
38
    {
39 15
        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 11
    public function remove(string $key): bool
99
    {
100 11
        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
                yield $file;
125
            }
126
        }
127 2
    }
128
129 4
    public function increment(string $key, int $step = 1): bool
130
    {
131
        try {
132 4
            $number = intval($this->load($key)) + $step;
133 2
        } catch (StorageException $ex) {
134
            // no file
135 2
            $number = 1;
136
        }
137 4
        $this->remove($key); // hanging pointers
138 4
        return $this->save($key, $number);
139
    }
140
141 4
    public function decrement(string $key, int $step = 1): bool
142
    {
143
        try {
144 4
            $number = intval($this->load($key)) - $step;
145 2
        } catch (StorageException $ex) {
146
            // no file
147 2
            $number = 0;
148
        }
149 4
        $this->remove($key); // hanging pointers
150 4
        return $this->save($key, $number);
151
    }
152
}
153