Passed
Push — master ( d467ff...321125 )
by Petr
08:04
created

AFiles::saveFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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