|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_storage\Storage\Target; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_storage\StorageException; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class VolumeStream |
|
11
|
|
|
* @package kalanis\kw_storage\Storage\Target |
|
12
|
|
|
* Store content onto volume - streams |
|
13
|
|
|
*/ |
|
14
|
|
|
class VolumeStream extends Volume |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $key |
|
18
|
|
|
* @throws StorageException |
|
19
|
|
|
* @return resource |
|
20
|
|
|
*/ |
|
21
|
4 |
|
public function load(string $key) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$content = @fopen($key, 'rb'); |
|
24
|
4 |
|
$data = @fopen('php://temp', 'r+b'); |
|
25
|
4 |
|
if ((false === $content) || (false === $data)) { |
|
26
|
|
|
// @codeCoverageIgnoreStart |
|
27
|
|
|
throw new StorageException('Cannot read file'); |
|
28
|
|
|
} |
|
29
|
|
|
// @codeCoverageIgnoreEnd |
|
30
|
3 |
|
if (false === @stream_copy_to_stream($content, $data)) { |
|
31
|
|
|
// @codeCoverageIgnoreStart |
|
32
|
|
|
throw new StorageException('Cannot read file'); |
|
33
|
|
|
} |
|
34
|
|
|
if (false === @fclose($content)) { |
|
35
|
|
|
// @codeCoverageIgnoreStart |
|
36
|
|
|
throw new StorageException('Cannot close opened file'); |
|
37
|
|
|
} |
|
38
|
|
|
// @codeCoverageIgnoreEnd |
|
39
|
3 |
|
return $data; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $key |
|
44
|
|
|
* @param resource $data |
|
45
|
|
|
* @param int|null $timeout |
|
46
|
|
|
* @throws StorageException |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function save(string $key, $data, ?int $timeout = null): bool |
|
50
|
|
|
{ |
|
51
|
2 |
|
$content = @fopen($key, 'wb'); |
|
52
|
2 |
|
if (false === $content) { |
|
53
|
|
|
// @codeCoverageIgnoreStart |
|
54
|
|
|
throw new StorageException('Cannot open file'); |
|
55
|
|
|
} |
|
56
|
|
|
// @codeCoverageIgnoreEnd |
|
57
|
2 |
|
if (false === @stream_copy_to_stream($data, $content, -1, 0)) { |
|
58
|
|
|
// @codeCoverageIgnoreStart |
|
59
|
|
|
throw new StorageException('Cannot save file'); |
|
60
|
|
|
} |
|
61
|
|
|
// @codeCoverageIgnoreEnd |
|
62
|
2 |
|
if (-1 === @fseek($content, 0)) { |
|
63
|
|
|
// @codeCoverageIgnoreStart |
|
64
|
|
|
throw new StorageException('Cannot seek in file'); |
|
65
|
|
|
} |
|
66
|
|
|
// @codeCoverageIgnoreEnd |
|
67
|
2 |
|
if (false === @fclose($content)) { |
|
68
|
|
|
// @codeCoverageIgnoreStart |
|
69
|
|
|
throw new StorageException('Cannot close opened file'); |
|
70
|
|
|
} |
|
71
|
|
|
// @codeCoverageIgnoreEnd |
|
72
|
2 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function increment(string $key, int $step = 1): bool |
|
76
|
|
|
{ |
|
77
|
|
|
try { |
|
78
|
2 |
|
$number = intval(Volume::load($key)) + $step; |
|
79
|
1 |
|
} catch (StorageException $ex) { |
|
80
|
|
|
// no file |
|
81
|
1 |
|
$number = 1; |
|
82
|
|
|
} |
|
83
|
2 |
|
$this->remove($key); // hanging pointers |
|
84
|
2 |
|
return Volume::save($key, $number); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
public function decrement(string $key, int $step = 1): bool |
|
88
|
|
|
{ |
|
89
|
|
|
try { |
|
90
|
2 |
|
$number = intval(Volume::load($key)) - $step; |
|
91
|
1 |
|
} catch (StorageException $ex) { |
|
92
|
|
|
// no file |
|
93
|
1 |
|
$number = 0; |
|
94
|
|
|
} |
|
95
|
2 |
|
$this->remove($key); // hanging pointers |
|
96
|
2 |
|
return Volume::save($key, $number); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|