Volume::truncate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage;
4
5
6
use kalanis\UploadPerPartes\Interfaces\ITemporaryStorage;
7
use kalanis\UploadPerPartes\Interfaces\IUppTranslations;
8
use kalanis\UploadPerPartes\Traits\TLang;
9
use kalanis\UploadPerPartes\UploadException;
10
11
12
/**
13
 * Class Volume
14
 * @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage
15
 * Storing driving file data on file system volume
16
 */
17
class Volume implements ITemporaryStorage
18
{
19
    use TLang;
20
21
    protected string $pathPrefix = '';
22
23 7
    public function __construct(string $pathPrefix = '', IUppTranslations $lang = null)
24
    {
25 7
        $this->pathPrefix = $pathPrefix;
26 7
        $this->setUppLang($lang);
27 7
    }
28
29 1
    public function exists(string $path): bool
30
    {
31 1
        return @file_exists($this->fullPath($path));
32
    }
33
34 2
    public function readData(string $path, ?int $fromByte, ?int $length): string
35
    {
36 2
        if (PHP_VERSION_ID > 77000 || !is_null($length)) {
37 1
            $content = @file_get_contents($this->fullPath($path), false, null, intval($fromByte), $length);
38
        } else {
39 2
            $content = @file_get_contents($this->fullPath($path), false, null, intval($fromByte));
40
        }
41 2
        if (false === $content) {
42 1
            throw new UploadException($this->getUppLang()->uppCannotReadFile($path));
43
        }
44 1
        return $content;
45
    }
46
47 2
    public function truncate(string $path, int $fromByte): bool
48
    {
49 2
        $sourceStream = @fopen($this->fullPath($path), 'rb+');
50 2
        if (!$sourceStream) {
0 ignored issues
show
introduced by
$sourceStream is of type false|resource, thus it always evaluated to false.
Loading history...
51 1
            throw new UploadException($this->getUppLang()->uppCannotWriteFile($path));
52
        }
53 1
        $a1 = @ftruncate($sourceStream, $fromByte);
54 1
        $a2 = @fclose($sourceStream);
55 1
        return $a1 && $a2;
56
    }
57
58 1
    public function append(string $path, string $content): bool
59
    {
60 1
        return false !== @file_put_contents($this->fullPath($path), $content, FILE_APPEND);
61
    }
62
63 2
    public function readStream(string $path)
64
    {
65 2
        $sourceStream = @fopen($this->fullPath($path), 'rb+');
66 2
        if (!$sourceStream) {
0 ignored issues
show
introduced by
$sourceStream is of type false|resource, thus it always evaluated to false.
Loading history...
67 1
            throw new UploadException($this->getUppLang()->uppCannotReadFile($path));
68
        }
69 1
        return $sourceStream;
70
    }
71
72 1
    public function remove(string $key): bool
73
    {
74 1
        return @unlink($this->fullPath($key));
75
    }
76
77 4
    protected function fullPath(string $path): string
78
    {
79 4
        return $this->pathPrefix . $path;
80
    }
81
}
82