Files::exists()   A
last analyzed

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\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 9
    public function __construct(CompositeAdapter $files, array $targetDir = [], ?ArrayPath $arrayPaths = null, ?Interfaces\IUppTranslations $lang = null)
37
    {
38 9
        $this->files = $files;
39 9
        $this->targetDir = $targetDir;
40 9
        $this->paths = $arrayPaths ?: new ArrayPath();
41 9
        $this->setUppLang($lang);
42 9
    }
43
44 2
    public function exists(string $key): bool
45
    {
46
        try {
47 2
            return $this->files->exists($this->fullPath($key));
48 1
        } catch (FilesException | PathsException $ex) {
49 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
50
        }
51
    }
52
53 3
    public function store(string $key, string $data): string
54
    {
55
        try {
56 3
            if (!$this->files->saveFile($this->fullPath($key), $data)) {
57 1
                throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key));
58
            }
59 1
            return $key;
60 2
        } catch (FilesException | PathsException $ex) {
61 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
62
        }
63
    }
64
65 2
    public function get(string $key): string
66
    {
67
        try {
68 2
            return $this->files->readFile($this->fullPath($key));
69 1
        } catch (FilesException | PathsException $ex) {
70 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
71
        }
72
    }
73
74 2
    public function remove(string $key): bool
75
    {
76
        try {
77 2
            return $this->files->deleteFile($this->fullPath($key));
78 1
        } catch (FilesException | PathsException $ex) {
79 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
80
        }
81
    }
82
83
    /**
84
     * @param string $key
85
     * @throws PathsException
86
     * @return string[]
87
     */
88 6
    protected function fullPath(string $key): array
89
    {
90 6
        return array_merge($this->targetDir, $this->paths->setString($key)->getArray());
91
    }
92
93 2
    public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool
94
    {
95 2
        if (!$encoder instanceof Interfaces\Storages\ForFiles) {
96 1
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder)));
97
        }
98 1
        return true;
99
    }
100
}
101