Storage::append()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Traits\TToStream;
8
use kalanis\kw_storage\Interfaces\IStorage;
9
use kalanis\kw_storage\StorageException;
10
use kalanis\UploadPerPartes\Interfaces\ITemporaryStorage;
11
use kalanis\UploadPerPartes\Interfaces\IUppTranslations;
12
use kalanis\UploadPerPartes\Traits\TLang;
13
use kalanis\UploadPerPartes\UploadException;
14
15
16
/**
17
 * Class Storage
18
 * @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage
19
 * Storing driving file data in simple storage represented by kw_storage package
20
 */
21
class Storage implements ITemporaryStorage
22
{
23
    use TLang;
24
    use TToStream;
25
26
    protected IStorage $storage;
27
    protected string $keyPrefix = '';
28
29 26
    public function __construct(IStorage $storage, string $keyPrefix = '', IUppTranslations $lang = null)
30
    {
31 26
        $this->storage = $storage;
32 26
        $this->keyPrefix = $keyPrefix;
33 26
        $this->setUppLang($lang);
34 26
    }
35
36 16
    public function exists(string $path): bool
37
    {
38
        try {
39 16
            return $this->storage->exists($this->fullPath($path));
40 1
        } catch (StorageException $ex) {
41 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
42
        }
43
    }
44
45 4
    public function readData(string $path, ?int $fromByte, ?int $length): string
46
    {
47
        try {
48 4
            $fullPath = $this->fullPath($path);
49 4
            $current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : '';
50 3
            return is_null($length)
51 1
                ? substr($current, intval($fromByte))
52 3
                : substr($current, intval($fromByte), $length)
53
            ;
54 1
        } catch (StorageException $ex) {
55 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
56
        }
57
    }
58
59 3
    public function truncate(string $path, int $fromByte): bool
60
    {
61
        try {
62 3
            $fullPath = $this->fullPath($path);
63 3
            $current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : '';
64 2
            return $this->storage->write($fullPath, substr($current, 0, $fromByte));
65 1
        } catch (StorageException $ex) {
66 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
67
        }
68
    }
69
70 5
    public function append(string $path, string $content): bool
71
    {
72
        try {
73 5
            $fullPath = $this->fullPath($path);
74 5
            $current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : '';
75 4
            return $this->storage->write($fullPath, $current . $content);
76 1
        } catch (StorageException $ex) {
77 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
78
        }
79
    }
80
81 7
    public function readStream(string $path)
82
    {
83
        try {
84 7
            $fullPath = $this->fullPath($path);
85 7
            $current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : '';
86 6
            return $this->toStream($path, $current);
87 1
        } catch (FilesException | StorageException $ex) {
88 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
89
        }
90
    }
91
92 12
    public function remove(string $key): bool
93
    {
94
        try {
95 12
            return $this->storage->remove($this->fullPath($key));
96 1
        } catch (StorageException $ex) {
97 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
98
        }
99
    }
100
101 21
    protected function fullPath(string $path): string
102
    {
103 21
        return $this->keyPrefix . $path;
104
    }
105
}
106