Passed
Push — master ( 2b0006...a1b0fe )
by Petr
02:56
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\Interfaces\IPassDirs;
8
use kalanis\kw_storage\Interfaces\IStorage;
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 IStorage, IPassDirs
19
{
20
    use TOperations;
21
    use TRemoveCycle;
22
23 4
    public function check(string $key): bool
24
    {
25 4
        $sepPos = mb_strrpos($key, DIRECTORY_SEPARATOR);
26 4
        $path = (false === $sepPos) ? substr($key, 0) : substr($key, 0, intval($sepPos));
27 4
        if (!is_dir($path)) {
28 2
            if (file_exists($path)) {
29 1
                unlink($path);
30
            }
31 2
            return mkdir($path, 0777);
32
        }
33 2
        return true;
34
    }
35
36 9
    public function exists(string $key): bool
37
    {
38 9
        return file_exists($key);
39
    }
40
41 5
    public function isDir(string $key): bool
42
    {
43 5
        return is_dir($key);
44
    }
45
46 1
    public function mkDir(string $key, bool $recursive = false): bool
47
    {
48 1
        return mkdir($key, 0777, $recursive);
49
    }
50
51 1
    public function rmDir(string $key, bool $recursive = false): bool
52
    {
53 1
        return $recursive ? $this->removeCycle($key) && rmdir($key) : rmdir($key);
54
    }
55
56 6
    public function load(string $key)
57
    {
58 6
        $content = @file_get_contents($key);
59 6
        if (false === $content) {
60 3
            throw new StorageException('Cannot read file');
61
        }
62 5
        return $content;
63
    }
64
65 6
    public function save(string $key, $data, ?int $timeout = null): bool
66
    {
67 6
        return (false !== @file_put_contents($key, strval($data)));
68
    }
69
70 7
    public function remove(string $key): bool
71
    {
72 7
        return @unlink($key);
73
    }
74
75 1
    public function lookup(string $path): Traversable
76
    {
77 1
        $real = realpath($path);
78 1
        if (false === $real) {
79 1
            return;
80
        }
81 1
        $files = scandir($real);
82 1
        if (!empty($files)) {
83 1
            foreach ($files as $file) {
84 1
                yield $file;
85
            }
86
        }
87
    }
88
89 2
    public function increment(string $key, int $step = 1): bool
90
    {
91
        try {
92 2
            $number = intval($this->load($key)) + $step;
93 1
        } catch (StorageException $ex) {
94
            // no file
95 1
            $number = 1;
96
        }
97 2
        $this->remove($key); // hanging pointers
98 2
        return $this->save($key, $number);
99
    }
100
101 2
    public function decrement(string $key, int $step = 1): bool
102
    {
103
        try {
104 2
            $number = intval($this->load($key)) - $step;
105 1
        } catch (StorageException $ex) {
106
            // no file
107 1
            $number = 0;
108
        }
109 2
        $this->remove($key); // hanging pointers
110 2
        return $this->save($key, $number);
111
    }
112
}
113