Volume::fullPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage;
4
5
6
use kalanis\UploadPerPartes\Interfaces;
7
use kalanis\UploadPerPartes\Target\Local\DrivingFile;
8
use kalanis\UploadPerPartes\Traits\TLang;
9
use kalanis\UploadPerPartes\UploadException;
10
11
12
/**
13
 * Class Volume
14
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\Storage
15
 * Processing info file on disk volume
16
 */
17
class Volume implements Interfaces\IDrivingFile
18
{
19
    use TLang;
20
21
    protected string $keyPrefix;
22
23 8
    public function __construct(string $keyPrefix = '', ?Interfaces\IUppTranslations $lang = null)
24
    {
25 8
        $this->keyPrefix = $keyPrefix;
26 8
        $this->setUppLang($lang);
27 8
    }
28
29 1
    public function exists(string $key): bool
30
    {
31 1
        return is_file($this->fullPath($key));
32
    }
33
34 2
    public function get(string $key): string
35
    {
36 2
        $content = @file_get_contents($this->fullPath($key));
37 2
        if (false === $content) {
38 1
            throw new UploadException($this->getUppLang()->uppDriveFileCannotRead($key));
39
        }
40 1
        return $content;
41
    }
42
43 2
    public function store(string $key, string $data): string
44
    {
45 2
        if (false === @file_put_contents($this->fullPath($key), $data)) {
46 1
            throw new UploadException($this->getUppLang()->uppDriveFileCannotWrite($key));
47
        }
48 1
        return $key;
49
    }
50
51 1
    public function remove(string $key): bool
52
    {
53 1
        return @unlink($this->fullPath($key));
54
    }
55
56
    /**
57
     * @param string $key
58
     * @return string
59
     */
60 3
    protected function fullPath(string $key): string
61
    {
62 3
        return $this->keyPrefix . $key;
63
    }
64
65 3
    public function checkKeyEncoder(DrivingFile\KeyEncoders\AEncoder $encoder): bool
66
    {
67 3
        if (!$encoder instanceof Interfaces\Storages\ForVolume) {
68 1
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($encoder)));
69
        }
70 2
        return true;
71
    }
72
}
73