Passed
Push — master ( 79545d...728467 )
by Petr
02:28
created

Basic::noDirectoryDelimiterSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_files\Processing\Storage\Files;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_paths\ArrayPath;
9
use kalanis\kw_storage\Interfaces\IStorage;
10
use kalanis\kw_storage\StorageException;
11
12
13
/**
14
 * Class Basic
15
 * @package kalanis\kw_files\Processing\Storage\Files
16
 * Process files via lookup
17
 */
18
class Basic extends AFiles
19
{
20 25
    public function __construct(IStorage $storage, ?IFLTranslations $lang = null)
21
    {
22 25
        $this->storage = $storage;
23 25
        $this->setLang($lang);
24 25
    }
25
26 7
    public function copyFile(array $source, array $dest): bool
27
    {
28 7
        $src = $this->getStorageSeparator() . $this->filledName($this->compactName($source, $this->getStorageSeparator()));
29 7
        $dst = $this->getStorageSeparator() . $this->filledName($this->compactName($dest, $this->getStorageSeparator()));
30
        try {
31 7
            if ($this->storage->exists($dst)) {
32 2
                return false;
33
            }
34 3
            $dstArr = new ArrayPath();
35 3
            $dstArr->setArray($dest);
36 3
            $tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator());
37 3
            if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) {
38 1
                return false;
39
            }
40
41 2
            return $this->storage->write($dst, $this->storage->read($src));
42 3
        } catch (StorageException $ex) {
43 3
            throw new FilesException($this->getLang()->flCannotCopyFile($src, $dst), $ex->getCode(), $ex);
44
        }
45
    }
46
47 5
    public function moveFile(array $source, array $dest): bool
48
    {
49 5
        $v1 = $this->copyFile($source, $dest);
50 3
        $v2 = $this->deleteFile($source);
51 3
        return $v1 && $v2;
52
    }
53
54
    /**
55
     * @return string
56
     * @codeCoverageIgnore only when path fails
57
     */
58
    protected function noDirectoryDelimiterSet(): string
59
    {
60
        return $this->getLang()->flNoDirectoryDelimiterSet();
61
    }
62
}
63