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

CanDirFlat::getSize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.7414

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 20
rs 9.8666
c 1
b 0
f 1
ccs 4
cts 12
cp 0.3333
crap 8.7414
1
<?php
2
3
namespace kalanis\kw_files\Processing\Storage\Dirs;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_files\Interfaces\ITypes;
9
use kalanis\kw_files\Node;
10
use kalanis\kw_paths\ArrayPath;
11
use kalanis\kw_paths\PathsException;
12
use kalanis\kw_storage\Interfaces\IPassDirs;
13
use kalanis\kw_storage\StorageException;
14
15
16
/**
17
 * Class CanDirDirect
18
 * @package kalanis\kw_files\Processing\Storage\Dirs
19
 * Process dirs via predefined api
20
 */
21
class CanDirFlat extends ADirs
22
{
23
    /** @var IPassDirs */
24
    protected $storage = null;
25
26 23
    public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null)
27
    {
28 23
        $this->storage = $storage;
29 23
        $this->setLang($lang);
30 23
    }
31
32 4
    public function createDir(array $entry, bool $deep = false): bool
33
    {
34 4
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
35
        try {
36 4
            return $this->storage->mkDir($path, $deep);
37 1
        } catch (StorageException $ex) {
38 1
            throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex);
39
        }
40
    }
41
42
    /**
43
     * @param string[] $entry
44
     * @param bool $loadRecursive
45
     * @param bool $wantSize
46
     * @throws FilesException
47
     * @throws PathsException
48
     * @return Node[]
49
     */
50 6
    public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false): array
51
    {
52 6
        $entryPath = $this->removeSeparator($this->compactName(array_filter($entry), $this->getStorageSeparator()));
53 6
        $entryPath = empty($entryPath) ? '' : $this->getStorageSeparator() . $entryPath;
54
        try {
55 6
            if (!$this->isNode($entryPath)) {
56 1
                throw new FilesException($this->getLang()->flCannotReadDir($entryPath));
57
            }
58
            /** @var array<string, Node> */
59 4
            $files = [];
60 4
            $sepLen = mb_strlen($this->getStorageSeparator());
61 4
            foreach ($this->storage->lookup($entryPath) as $item) {
62 4
                $usePath = mb_substr($item, $sepLen);
63 4
                if (!$loadRecursive && (false !== mb_strpos($usePath, $this->getStorageSeparator()))) {
64
                    // pass sub when not need
65 2
                    continue;
66
                }
67
68 4
                $sub = new Node();
69 4
                $currentPath = $this->removeSeparator($this->compactName(array_merge(
70 4
                    $entry,
71 4
                    $this->expandName($item, $this->getStorageSeparator())
72 4
                ), $this->getStorageSeparator()));
73 4
                if (empty($item)) {
74 4
                    $sub->setData(
75 4
                        [],
76 4
                        0,
77 4
                        ITypes::TYPE_DIR
78
                    );
79 4
                } elseif ($this->isNode($this->getStorageSeparator(). $currentPath)) {
80 3
                    $sub->setData(
81 3
                        $this->expandName($usePath),
82 3
                        0,
83 3
                        ITypes::TYPE_DIR
84
                    );
85
                } else {
86
                    // normal node - file
87 4
                    $sub->setData(
88 4
                        $this->expandName($usePath),
89 4
                        $wantSize ? $this->getSize($this->getStorageSeparator() . $currentPath) : 0,
90 4
                        ITypes::TYPE_FILE
91
                    );
92
                }
93 4
                $files[] = $sub;
94
            }
95 4
            return $files;
96 2
        } catch (StorageException $ex) {
97 1
            throw new FilesException($this->getLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex);
98
        }
99
    }
100
101 6
    public function copyDir(array $source, array $dest): bool
102
    {
103 6
        $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator());
104 6
        $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator());
105
        try {
106 6
            if ($this->isSubPart($dest, $source)) {
107 1
                return false;
108
            }
109
110 5
            if (!$this->isNode($src)) {
111 1
                return false;
112
            }
113 3
            if ($this->storage->exists($dst)) {
114 1
                return false;
115
            }
116 2
            $dstArr = new ArrayPath();
117 2
            $dstArr->setArray($dest);
118 2
            if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) {
119 1
                return false;
120
            }
121
122 1
            return $this->storage->copy($src, $dst);
123 1
        } catch (StorageException $ex) {
124 1
            throw new FilesException($this->getLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex);
125
        }
126
    }
127
128 6
    public function moveDir(array $source, array $dest): bool
129
    {
130 6
        $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator());
131 6
        $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator());
132
        try {
133 6
            if ($this->isSubPart($dest, $source)) {
134 1
                return false;
135
            }
136
137 5
            if (!$this->isNode($src)) {
138 1
                return false;
139
            }
140 3
            if ($this->storage->exists($dst)) {
141 1
                return false;
142
            }
143 2
            $dstArr = new ArrayPath();
144 2
            $dstArr->setArray($dest);
145 2
            if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) {
146 1
                return false;
147
            }
148
149 1
            return $this->storage->move($src, $dst);
150 1
        } catch (StorageException $ex) {
151 1
            throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex);
152
        }
153
    }
154
155 5
    public function deleteDir(array $entry, bool $deep = false): bool
156
    {
157 5
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
158
        try {
159 5
            if ($this->storage->isDir($path)) {
160 3
                return $this->storage->rmDir($path, $deep);
161
            } else {
162 2
                return false;
163
            }
164 1
        } catch (StorageException $ex) {
165 1
            throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex);
166
        }
167
    }
168
169 6
    protected function removeSeparator(string $path): string
170
    {
171 6
        $sepLen = mb_strlen($this->getStorageSeparator());
172 6
        $first = mb_substr($path, 0, $sepLen);
173 6
        return $this->getStorageSeparator() == $first ? mb_substr($path, $sepLen) : $path;
174
    }
175
176
    /**
177
     * @param string $entry
178
     * @throws StorageException
179
     * @return bool
180
     */
181 15
    protected function isNode(string $entry): bool
182
    {
183 15
        return $this->storage->exists($entry) && $this->storage->isDir($entry);
184
    }
185
186
    /**
187
     * @param string $file
188
     * @throws FilesException
189
     * @throws StorageException
190
     * @return int
191
     */
192 1
    protected function getSize(string $file): int
193
    {
194 1
        $content = $this->storage->read($file);
195 1
        if (is_resource($content)) {
196
            // @codeCoverageIgnoreStart
197
            // usually not need to use
198
            // a bit workaround
199
            $tempStream = fopen("php://temp", "w+b");
200
            if (false === $tempStream) {
201
                throw new FilesException($this->getLang()->flCannotLoadFile($file));
202
            }
203
            rewind($content);
204
            $size = stream_copy_to_stream($content, $tempStream, -1, 0);
205
            if (false === $size) {
206
                throw new FilesException($this->getLang()->flCannotGetSize($file));
207
            }
208
            return intval($size);
209
        } else {
210
        // @codeCoverageIgnoreEnd
211 1
            return strlen(strval($content));
212
        }
213
    }
214
215
    /**
216
     * @return string
217
     * @codeCoverageIgnore only when path fails
218
     */
219
    protected function noDirectoryDelimiterSet(): string
220
    {
221
        return $this->getLang()->flNoDirectoryDelimiterSet();
222
    }
223
}
224