Passed
Push — master ( d37de2...50254d )
by Petr
09:41
created

Files::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\InfoStorage;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Traits\TToString;
9
use kalanis\kw_paths\PathsException;
10
use kalanis\UploadPerPartes\Exceptions\UploadException;
11
use kalanis\UploadPerPartes\Interfaces\IUPPTranslations;
12
13
14
/**
15
 * Class Files
16
 * @package kalanis\UploadPerPartes\InfoStorage
17
 * Processing info file on some file storage
18
 */
19
class Files extends AStorage
20
{
21
    use TToString;
22
23
    /** @var CompositeAdapter */
24
    protected $lib = null;
25
26 8
    public function __construct(CompositeAdapter $lib, ?IUPPTranslations $lang = null)
27
    {
28 8
        $this->lib = $lib;
29 8
        parent::__construct($lang);
30 8
    }
31
32 2
    public function exists(string $key): bool
33
    {
34
        try {
35 2
            return $this->lib->exists([$key]);
36 1
        } catch (FilesException | PathsException $ex) {
37 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
38
        }
39
    }
40
41 3
    public function load(string $key): string
42
    {
43
        try {
44 3
            return $this->toString($key, $this->lib->readFile([$key]));
45 2
        } catch (FilesException | PathsException $ex) {
46 2
            throw new UploadException($this->getUppLang()->uppCannotReadFile($key), $ex->getCode(), $ex);
47
        }
48
    }
49
50 3
    public function save(string $key, string $data): void
51
    {
52
        try {
53 3
            $this->lib->saveFile([$key], $data);
54 2
        } catch (FilesException | PathsException $ex) {
55 2
            throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key), $ex->getCode(), $ex);
56
        }
57 1
    }
58
59 3
    public function remove(string $key): void
60
    {
61
        try {
62 3
            $this->lib->deleteFile([$key]);
63 2
        } catch (FilesException | PathsException $ex) {
64 2
            throw new UploadException($this->getUppLang()->uppDriveFileCannotRemove($key), $ex->getCode(), $ex);
65
        }
66 1
    }
67
}
68