Passed
Push — master ( 1f33ad...fba3db )
by Petr
08:38
created

AFiles::saveFile()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
nc 16
nop 3
dl 0
loc 26
rs 9.0444
c 1
b 0
f 0
ccs 16
cts 16
cp 1
crap 6
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\IProcessFiles;
8
use kalanis\kw_files\Traits\TLang;
9
use kalanis\kw_files\Traits\TStreamToPos;
10
use kalanis\kw_files\Traits\TToStream;
11
use kalanis\kw_paths\ArrayPath;
12
use kalanis\kw_paths\Extras\TPathTransform;
13
use kalanis\kw_storage\Interfaces\IPassDirs;
14
use kalanis\kw_storage\Interfaces\IStorage;
15
use kalanis\kw_storage\StorageException;
16
17
18
/**
19
 * Class AFiles
20
 * @package kalanis\kw_files\Processing\Storage\Files
21
 * Process files in storages - deffer when you can access them directly or must be a middleman there
22
 */
23
abstract class AFiles implements IProcessFiles
24
{
25
    use TLang;
26
    use TPathTransform;
27
    use TStreamToPos;
28
    use TToStream;
29
30
    /** @var IStorage|IPassDirs */
31
    protected $storage = null;
32
33 17
    public function saveFile(array $targetName, $content, ?int $offset = null): bool
34
    {
35 17
        $path = $this->getStorageSeparator() . $this->filledName($this->compactName($targetName, $this->getStorageSeparator()));
36
        try {
37 17
            $dstArr = new ArrayPath();
38 17
            $dstArr->setArray($targetName);
39 17
            $tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator());
40 17
            if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) {
41
                // parent dir
42 1
                throw new FilesException($this->getLang()->flCannotSaveFile($path));
43
            }
44
45 16
            $added = $this->toStream($path, $content);
46 16
            if (!is_null($offset)) {
47
                // put it somewhere, left the rest intact
48 4
                if ($this->storage->exists($path)) {
49 2
                    $target = $this->toStream($path, $this->storage->read($path));
50
                } else {
51 2
                    $target = $this->toStream('', '');
52
                }
53 4
                return $this->storage->write($path, $this->addStreamToPosition($target, $added, $offset));
54
            } else {
55 14
                return $this->storage->write($path, $content);
56
            }
57 4
        } catch (StorageException $ex) {
58 3
            throw new FilesException($this->getLang()->flCannotSaveFile($path), $ex->getCode(), $ex);
59
        }
60
    }
61
62 8
    public function readFile(array $entry, ?int $offset = null, ?int $length = null)
63
    {
64 8
        $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator()));
65
        try {
66 8
            $content = $this->storage->read($path);
67 4
            if (false === $content) {
68 1
                throw new FilesException($this->getLang()->flCannotLoadFile($path));
69 3
            } elseif (is_resource($content)) {
70 1
                if (!is_null($length) || !is_null($offset)) {
71 1
                    $stream = fopen('php://temp', 'rb+');
72 1
                    rewind($content);
73 1
                    if (false === $stream) {
74
                        // @codeCoverageIgnoreStart
75
                        throw new FilesException($this->getLang()->flCannotLoadFile($path));
76
                    }
77
                    // @codeCoverageIgnoreEnd
78 1
                    if (false === stream_copy_to_stream($content, $stream, (is_null($length) ? -1 : $length), intval($offset))) {
79
                        // @codeCoverageIgnoreStart
80
                        throw new FilesException($this->getLang()->flCannotGetFilePart($path));
81
                    }
82
                    // @codeCoverageIgnoreEnd
83 1
                    return $stream;
84
                } else {
85 1
                    return $content;
86
                }
87
            } else {
88
                // shit with substr... that needed undefined params was from some java dude?!
89 2
                if (!is_null($length)) {
90 2
                    return mb_substr(strval($content), intval($offset), $length);
91
                }
92 2
                if (!is_null($offset)) {
93 2
                    return mb_substr(strval($content), $offset);
94
                }
95 2
                return strval($content);
96
            }
97 5
        } catch (StorageException $ex) {
98 4
            throw new FilesException($this->getLang()->flCannotLoadFile($path), $ex->getCode(), $ex);
99
        }
100
    }
101
102 6
    public function deleteFile(array $entry): bool
103
    {
104 6
        $path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator()));
105
        try {
106 6
            return $this->storage->remove($path);
107 2
        } catch (StorageException $ex) {
108 2
            throw new FilesException($this->getLang()->flCannotRemoveFile($path), $ex->getCode(), $ex);
109
        }
110
    }
111
112 37
    protected function getStorageSeparator(): string
113
    {
114 37
        return DIRECTORY_SEPARATOR;
115
    }
116
117 35
    protected function filledName(string $path): string
118
    {
119 35
        $sepLen = mb_strlen($this->getStorageSeparator());
120 35
        $beginning = mb_substr($path, 0, $sepLen);
121 35
        return ($this->getStorageSeparator() == $beginning) ? mb_substr($path, $sepLen) : $path;
122
    }
123
}
124