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

Files::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 7
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\Extended\FindFreeName;
8
use kalanis\kw_files\FilesException;
9
use kalanis\kw_paths\ArrayPath;
10
use kalanis\kw_paths\PathsException;
11
use kalanis\kw_paths\Stuff;
12
use kalanis\UploadPerPartes\Interfaces\IFinalStorage;
13
use kalanis\UploadPerPartes\Interfaces\IUppTranslations;
14
use kalanis\UploadPerPartes\Traits\TLang;
15
use kalanis\UploadPerPartes\UploadException;
16
17
18
/**
19
 * Class Files
20
 * @package kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage
21
 * Where to store data on target destination - storage based on kw_files
22
 */
23
class Files implements IFinalStorage
24
{
25
    use TLang;
26
27
    /** @var string[] */
28
    protected array $targetDir = [];
29
    protected CompositeAdapter $files;
30
    protected FindFreeName $freeName;
31
    protected ArrayPath $paths;
32
33
    /**
34
     * @param CompositeAdapter $files
35
     * @param string[] $targetDir
36
     * @param ArrayPath|null $arrayPaths
37
     * @param IUppTranslations|null $lang
38
     */
39
    public function __construct(CompositeAdapter $files, array $targetDir = [], ?ArrayPath $arrayPaths = null, IUppTranslations $lang = null)
40
    {
41
        $this->files = $files;
42
        $this->targetDir = $targetDir;
43
        $this->paths = $arrayPaths ?: new ArrayPath();
44
        $this->freeName = new FindFreeName($files);
45
        $this->setUppLang($lang);
46
    }
47
48
    public function exists(string $key): bool
49
    {
50
        try {
51
            return $this->files->exists($this->fullPath($key));
52
        } catch (FilesException | PathsException $ex) {
53
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
54
        }
55
    }
56
57
    public function store(string $key, $data): bool
58
    {
59
        try {
60
            return $this->files->saveFileStream($this->fullPath($key), $data);
61
        } catch (FilesException | PathsException $ex) {
62
            throw new UploadException($this->getUppLang()->uppCannotWriteFile($key), $ex->getCode(), $ex);
63
        }
64
    }
65
66
    public function findName(string $key): string
67
    {
68
        try {
69
            $this->paths->setString($key);
70
            $fileName = $this->paths->getFileName();
71
            $directory = $this->paths->getArrayDirectory();
72
            $freeName = $this->freeName->findFreeName(
73
                array_merge($this->targetDir, $directory),
74
                Stuff::fileBase($fileName),
75
                '.' . Stuff::fileExt($fileName)
76
            );
77
            return $this->paths->setArray(array_merge(
78
                $directory,
79
                [$freeName]
80
            ))->getString();
81
        } catch (FilesException | PathsException $ex) {
82
            throw new UploadException($this->getUppLang()->uppCannotWriteFile($key), $ex->getCode(), $ex);
83
        }
84
    }
85
86
    /**
87
     * @param string $key
88
     * @throws PathsException
89
     * @return string[]
90
     */
91
    protected function fullPath(string $key): array
92
    {
93
        return array_merge($this->targetDir, $this->paths->setString($key)->getArray());
94
    }
95
}
96