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

Volume   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
A getNameSeparator() 0 3 1
A findName() 0 14 3
A fullPath() 0 3 1
A __construct() 0 4 1
A store() 0 4 1
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage;
4
5
6
use kalanis\kw_paths\Stuff;
7
use kalanis\UploadPerPartes\Interfaces\IFinalStorage;
8
use kalanis\UploadPerPartes\Interfaces\IUppTranslations;
9
use kalanis\UploadPerPartes\Traits\TLang;
10
11
12
/**
13
 * Class Files
14
 * @package kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage
15
 * Where to store data on target destination - storage is local volume
16
 */
17
class Volume implements IFinalStorage
18
{
19
    use TLang;
20
21
    protected string $pathPrefix = '';
22
23
    public function __construct(string $pathPrefix = '', IUppTranslations $lang = null)
24
    {
25
        $this->pathPrefix = $pathPrefix;
26
        $this->setUppLang($lang);
27
    }
28
29
    public function exists(string $key): bool
30
    {
31
        return @file_exists($this->fullPath($key));
32
    }
33
34
    public function store(string $key, $data): bool
35
    {
36
        @rewind($data);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rewind(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

36
        /** @scrutinizer ignore-unhandled */ @rewind($data);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
37
        return false !== @file_put_contents($this->fullPath($key), $data);
38
    }
39
40
    public function findName(string $key): string
41
    {
42
        if (!$this->exists($key)) {
43
            return $key;
44
        }
45
46
        $name = Stuff::fileBase($key);
47
        $suffix = Stuff::fileExt($key);
48
49
        $i = 0;
50
        while ($this->exists($name . $this->getNameSeparator() . strval($i) . '.' . $suffix)) {
51
            $i++;
52
        }
53
        return $name . $this->getNameSeparator() . strval($i) . '.' . $suffix;
54
    }
55
56
    protected function fullPath(string $key): string
57
    {
58
        return $this->pathPrefix . $key;
59
    }
60
61
    protected function getNameSeparator(): string
62
    {
63
        return '__';
64
    }
65
}
66