Passed
Push — master ( d467ff...321125 )
by Petr
08:04
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\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