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

Files::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_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_paths\ArrayPath;
9
use kalanis\kw_paths\PathsException;
10
use kalanis\UploadPerPartes\Interfaces;
11
use kalanis\UploadPerPartes\Target\Local\DrivingFile;
12
use kalanis\UploadPerPartes\Traits\TLang;
13
use kalanis\UploadPerPartes\UploadException;
14
15
16
/**
17
 * Class Files
18
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage
19
 * Storing driving file data in files system - based on kw_files
20
 */
21
class Files implements Interfaces\IDrivingFile
22
{
23
    use TLang;
24
25
    /** @var string[] */
26
    protected array $targetDir;
27
    protected CompositeAdapter $files;
28
    protected ArrayPath $paths;
29
30
    /**
31
     * @param CompositeAdapter $files
32
     * @param string[] $targetDir
33
     * @param ArrayPath|null $arrayPaths
34
     * @param Interfaces\IUppTranslations|null $lang
35
     */
36
    public function __construct(CompositeAdapter $files, array $targetDir = [], ?ArrayPath $arrayPaths = null, ?Interfaces\IUppTranslations $lang = null)
37
    {
38
        $this->files = $files;
39
        $this->targetDir = $targetDir;
40
        $this->paths = $arrayPaths ?: new ArrayPath();
41
        $this->setUppLang($lang);
42
    }
43
44
    public function exists(string $key): bool
45
    {
46
        try {
47
            return $this->files->exists($this->fullPath($key));
48
        } catch (FilesException | PathsException $ex) {
49
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
50
        }
51
    }
52
53
    public function store(string $key, string $data): string
54
    {
55
        try {
56
            if (!$this->files->saveFile($this->fullPath($key), $data)) {
57
                throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key));
58
            }
59
            return $key;
60
        } catch (FilesException | PathsException $ex) {
61
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
62
        }
63
    }
64
65
    public function get(string $key): string
66
    {
67
        try {
68
            return $this->files->readFile($this->fullPath($key));
69
        } catch (FilesException | PathsException $ex) {
70
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
71
        }
72
    }
73
74
    public function remove(string $key): bool
75
    {
76
        try {
77
            return $this->files->deleteFile($this->fullPath($key));
78
        } catch (FilesException | PathsException $ex) {
79
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
80
        }
81
    }
82
83
    /**
84
     * @param string $key
85
     * @throws PathsException
86
     * @return string[]
87
     */
88
    protected function fullPath(string $key): array
89
    {
90
        return array_merge($this->targetDir, $this->paths->setString($key)->getArray());
91
    }
92
93
    public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool
94
    {
95
        if (!$encoder instanceof Interfaces\Storages\ForFiles) {
96
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder)));
97
        }
98
        return true;
99
    }
100
}
101