Files::truncate()   B
last analyzed

Complexity

Conditions 7
Paths 17

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 17
nop 2
dl 0
loc 22
ccs 15
cts 16
cp 0.9375
crap 7.0119
rs 8.8333
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\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\ITemporaryStorage;
11
use kalanis\UploadPerPartes\Interfaces\IUppTranslations;
12
use kalanis\UploadPerPartes\Traits\TLang;
13
use kalanis\UploadPerPartes\UploadException;
14
15
16
/**
17
 * Class Files
18
 * @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage
19
 * Storing driving file data on files system - based on kw_files
20
 */
21
class Files implements ITemporaryStorage
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 IUppTranslations|null $lang
35
     */
36 8
    public function __construct(CompositeAdapter $files, array $targetDir = [], ?ArrayPath $arrayPaths = null, IUppTranslations $lang = null)
37
    {
38 8
        $this->files = $files;
39 8
        $this->targetDir = $targetDir;
40 8
        $this->paths = $arrayPaths ?: new ArrayPath();
41 8
        $this->setUppLang($lang);
42 8
    }
43
44 2
    public function exists(string $path): bool
45
    {
46
        try {
47 2
            return $this->files->exists($this->fullPath($path));
48 1
        } catch (FilesException | PathsException $ex) {
49 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
50
        }
51
    }
52
53 2
    public function readData(string $path, ?int $fromByte, ?int $length): string
54
    {
55
        try {
56 2
            return $this->files->readFile($this->fullPath($path), $fromByte, $length);
57 1
        } catch (FilesException | PathsException $ex) {
58 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
59
        }
60
    }
61
62 2
    public function truncate(string $path, int $fromByte): bool
63
    {
64
        try {
65 2
            $nameFull = $this->fullPath($path);
66 2
            $tempFull = $this->fullPath($path . '.TEMPORARY_FILE');
67 2
            $a = $this->files->moveFile($nameFull, $tempFull);
68 1
            $old = $this->files->readFileStream($tempFull);
69 1
            $b = rewind($old);
70 1
            $new = fopen('php://temp', 'rb+');
71 1
            if (!is_resource($new)) {
72
                // @codeCoverageIgnoreStart
73
                // phpstan
74
                throw new UploadException($this->getUppLang()->uppCannotReadFile('temp'));
75
            }
76
            // @codeCoverageIgnoreEnd
77 1
            $c = boolval(stream_copy_to_stream($old, $new, $fromByte));
78 1
            rewind($new);
79 1
            $d = $this->files->saveFileStream($nameFull, $new);
80 1
            $e = $this->files->deleteFile($tempFull);
81 1
            return $a && $b && $c && $d && $e;
82 1
        } catch (FilesException | PathsException $ex) {
83 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
84
        }
85
    }
86
87 2
    public function append(string $path, string $content): bool
88
    {
89
        try {
90 2
            return $this->files->saveFile($this->fullPath($path), $content, null, FILE_APPEND);
91 1
        } catch (FilesException | PathsException $ex) {
92 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
93
        }
94
    }
95
96 2
    public function readStream(string $path)
97
    {
98
        try {
99 2
            return $this->files->readFileStream($this->fullPath($path));
100 1
        } catch (FilesException | PathsException $ex) {
101 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
102
        }
103
    }
104
105 2
    public function remove(string $path): bool
106
    {
107
        try {
108 2
            return $this->files->deleteFile($this->fullPath($path));
109 1
        } catch (FilesException | PathsException $ex) {
110 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
111
        }
112
    }
113
114
    /**
115
     * @param string $key
116
     * @throws PathsException
117
     * @return string[]
118
     */
119 7
    protected function fullPath(string $key): array
120
    {
121 7
        return array_merge($this->targetDir, $this->paths->setString($key)->getArray());
122
    }
123
}
124