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

Files   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 46
ccs 21
cts 21
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 6 2
A __construct() 0 4 1
A exists() 0 6 2
A save() 0 6 2
A remove() 0 6 2
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