Test Failed
Push — master ( e493d7...b1ea68 )
by Petr
11:01
created

Storage::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage;
4
5
6
use kalanis\kw_storage\Interfaces\IStorage;
7
use kalanis\kw_storage\StorageException;
8
use kalanis\UploadPerPartes\Interfaces;
9
use kalanis\UploadPerPartes\Target\Local\DrivingFile;
10
use kalanis\UploadPerPartes\Traits\TLang;
11
use kalanis\UploadPerPartes\UploadException;
12
13
14
/**
15
 * Class Storage
16
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage
17
 * Storing driving file data in simple storage represented by kw_storage package
18
 */
19
class Storage implements Interfaces\IDrivingFile
20
{
21
    use TLang;
22
23
    protected IStorage $storage;
24
    protected string $keyPrefix;
25
26
    public function __construct(IStorage $storage, string $keyPrefix = '', ?Interfaces\IUppTranslations $lang = null)
27
    {
28
        $this->storage = $storage;
29
        $this->keyPrefix = $keyPrefix;
30
        $this->setUppLang($lang);
31
    }
32
33
    public function exists(string $key): bool
34
    {
35
        try {
36
            return $this->storage->exists($this->fullPath($key));
37
        } catch (StorageException $ex) {
38
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
39
        }
40
    }
41
42
    public function store(string $key, string $data): string
43
    {
44
        try {
45
            if (!$this->storage->write($this->fullPath($key), $data)) {
46
                throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key));
47
            }
48
            return $key;
49
        } catch (StorageException $ex) {
50
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
51
        }
52
    }
53
54
    public function get(string $key): string
55
    {
56
        try {
57
            return $this->storage->read($this->fullPath($key));
58
        } catch (StorageException $ex) {
59
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
60
        }
61
    }
62
63
    public function remove(string $key): bool
64
    {
65
        try {
66
            return $this->storage->remove($this->fullPath($key));
67
        } catch (StorageException $ex) {
68
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
69
        }
70
    }
71
72
    /**
73
     * @param string $key
74
     * @return string
75
     */
76
    protected function fullPath(string $key): string
77
    {
78
        return $this->keyPrefix . $key;
79
    }
80
81
    public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool
82
    {
83
        if (!$encoder instanceof Interfaces\Storages\ForStorage) {
84
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder)));
85
        }
86
        return true;
87
    }
88
}
89