|
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
|
28 |
|
public function __construct(IStorage $storage, string $keyPrefix = '', ?Interfaces\IUppTranslations $lang = null) |
|
27
|
|
|
{ |
|
28
|
28 |
|
$this->storage = $storage; |
|
29
|
28 |
|
$this->keyPrefix = $keyPrefix; |
|
30
|
28 |
|
$this->setUppLang($lang); |
|
31
|
28 |
|
} |
|
32
|
|
|
|
|
33
|
17 |
|
public function exists(string $key): bool |
|
34
|
|
|
{ |
|
35
|
|
|
try { |
|
36
|
17 |
|
return $this->storage->exists($this->fullPath($key)); |
|
37
|
1 |
|
} catch (StorageException $ex) { |
|
38
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
18 |
|
public function store(string $key, string $data): string |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
18 |
|
if (!$this->storage->write($this->fullPath($key), $data)) { |
|
46
|
1 |
|
throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key)); |
|
47
|
|
|
} |
|
48
|
16 |
|
return $key; |
|
49
|
2 |
|
} catch (StorageException $ex) { |
|
50
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
17 |
|
public function get(string $key): string |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
17 |
|
return $this->storage->read($this->fullPath($key)); |
|
58
|
3 |
|
} catch (StorageException $ex) { |
|
59
|
3 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
10 |
|
public function remove(string $key): bool |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
10 |
|
return $this->storage->remove($this->fullPath($key)); |
|
67
|
1 |
|
} catch (StorageException $ex) { |
|
68
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
23 |
|
protected function fullPath(string $key): string |
|
77
|
|
|
{ |
|
78
|
23 |
|
return $this->keyPrefix . $key; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
20 |
|
public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool |
|
82
|
|
|
{ |
|
83
|
20 |
|
if (!$encoder instanceof Interfaces\Storages\ForStorage) { |
|
84
|
1 |
|
throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder))); |
|
85
|
|
|
} |
|
86
|
19 |
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|