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

AFiles::readFile()   B

Complexity

Conditions 11
Paths 19

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.0908

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 1
b 0
f 0
nc 19
nop 3
dl 0
loc 37
ccs 20
cts 22
cp 0.9091
crap 11.0908
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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