Passed
Push — master ( 96f905...d7f7a4 )
by Petr
02:28
created

ProcessDir::setPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_files\Processing\Volume;
4
5
6
use FilesystemIterator;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Interfaces\IFLTranslations;
9
use kalanis\kw_files\Interfaces\IProcessDirs;
10
use kalanis\kw_files\Interfaces\ITypes;
11
use kalanis\kw_files\Node;
12
use kalanis\kw_files\Processing\TPath;
13
use kalanis\kw_files\Traits\TLang;
14
use kalanis\kw_files\Traits\TSubPart;
15
use kalanis\kw_paths\ArrayPath;
16
use kalanis\kw_paths\Extras\TPathTransform;
17
use kalanis\kw_paths\PathsException;
18
use kalanis\kw_storage\Extras\TRemoveCycle;
19
use kalanis\kw_storage\Extras\TVolumeCopy;
20
use RecursiveDirectoryIterator;
21
use RecursiveIteratorIterator;
22
use SplFileInfo;
23
use Throwable;
24
25
26
/**
27
 * Class ProcessDir
28
 * @package kalanis\kw_files\Processing\Volume
29
 * Process dirs in basic ways
30
 */
31
class ProcessDir implements IProcessDirs
32
{
33
    use TLang;
34
    use TPath;
35
    use TPathTransform;
36
    use TRemoveCycle;
37
    use TSubPart;
38
    use TVolumeCopy;
39
40
    /** @var int */
41
    protected $sliceStartParts = 0;
42
    /** @var int */
43
    protected $sliceCustomParts = 0;
44
45
    /**
46
     * @param string $path
47
     * @param IFLTranslations|null $lang
48
     * @throws PathsException
49
     */
50 23
    public function __construct(string $path = '', ?IFLTranslations $lang = null)
51
    {
52 23
        $this->setFlLang($lang);
53 23
        $this->setPath($path);
54 23
    }
55
56
    /**
57
     * @param string $path
58
     * @throws PathsException
59
     */
60 23
    public function setPath(string $path = ''): void
61
    {
62 23
        $used = realpath($path);
63 23
        if (false !== $used) {
64 22
            $this->path = $used;
65 22
            $this->sliceStartParts = count($this->expandName($used));
66
        }
67 23
    }
68
69 4
    public function createDir(array $entry, bool $deep = false): bool
70
    {
71 4
        $path = $this->fullPath($entry);
72
        try {
73 4
            return @mkdir($path, 0777, $deep);
74
            // @codeCoverageIgnoreStart
75
        } catch (Throwable $ex) {
76
            throw new FilesException($this->getFlLang()->flCannotCreateDir($path), $ex->getCode(), $ex);
77
        }
78
        // @codeCoverageIgnoreEnd
79
    }
80
81 4
    public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false): array
82
    {
83 4
        $path = $this->fullPath($entry);
84 4
        $this->sliceCustomParts = count($entry);
85 4
        if (!is_dir($path)) {
86 1
            throw new FilesException($this->getFlLang()->flCannotReadDir($path));
87
        }
88
89
        try {
90 3
            $iter = $loadRecursive
91 1
                ? new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))
92 3
                : new FilesystemIterator($path, 0)
93
            ;
94 3
            return array_values(
95 3
                array_merge(
96 3
                    $loadRecursive ? [] : [$this->addRootNode($path)],
97 3
                    array_map(
98 3
                        [$this, 'intoNode'],
99 3
                        array_filter(
100 3
                            array_filter(
101 3
                                array_filter(
102 3
                                    iterator_to_array($iter)
103
                                ),
104 3
                                [$this, 'filterFilesAndDirsOnly']
105
                            ),
106 3
                            [$this, 'filterDots']
107
                        )
108
                    )
109
                )
110
            );
111
            // @codeCoverageIgnoreStart
112
        } catch (Throwable $ex) {
113
            throw new FilesException($this->getFlLang()->flCannotReadDir($path), $ex->getCode(), $ex);
114
        }
115
        // @codeCoverageIgnoreEnd
116
    }
117
118
    /**
119
     * @param string $path
120
     * @throws PathsException
121
     * @return Node
122
     */
123 2
    protected function addRootNode(string $path): Node
124
    {
125 2
        return $this->intoNode(new SplFileInfo($path));
126
    }
127
128 3
    public function filterFilesAndDirsOnly(SplFileInfo $file): bool
129
    {
130 3
        return in_array($file->getType(), [ITypes::TYPE_DIR, ITypes::TYPE_FILE]);
131
    }
132
133 3
    public function filterDots(SplFileInfo $file): bool
134
    {
135 3
        return '..' !== $file->getFilename();
136
    }
137
138
    /**
139
     * @param SplFileInfo $file
140
     * @throws PathsException
141
     * @return Node
142
     */
143 3
    public function intoNode(SplFileInfo $file): Node
144
    {
145 3
        $node = new Node();
146 3
        return $node->setData(
147 3
            array_slice($this->expandName($file->getRealPath()), $this->sliceStartParts + $this->sliceCustomParts),
148 3
            $file->getSize(),
149 3
            $file->getType()
150
        );
151
    }
152
153 5
    public function copyDir(array $source, array $dest): bool
154
    {
155 5
        $src = $this->fullPath($source);
156 5
        $dst = $this->fullPath($dest);
157 5
        if ($this->isSubPart($dest, $source)) {
158 1
            return false;
159
        }
160
161 4
        if (!file_exists($src)) {
162 1
            return false;
163
        }
164
165 3
        $dstArr = new ArrayPath();
166 3
        $dstArr->setArray($dest);
167
168 3
        if (!file_exists($this->fullPath($dstArr->getArrayDirectory()))) {
169 1
            return false;
170
        }
171
172 2
        if (file_exists($dst)) {
173 1
            return false;
174
        }
175
176
        try {
177 1
            return $this->xcopy($src, $dst);
178
            // @codeCoverageIgnoreStart
179
        } catch (Throwable $ex) {
180
            throw new FilesException($this->getFlLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex);
181
        }
182
        // @codeCoverageIgnoreEnd
183
    }
184
185 5
    public function moveDir(array $source, array $dest): bool
186
    {
187 5
        $src = $this->fullPath($source);
188 5
        $dst = $this->fullPath($dest);
189 5
        if ($this->isSubPart($dest, $source)) {
190 1
            return false;
191
        }
192
193 4
        if (!file_exists($src)) {
194 1
            return false;
195
        }
196
197 3
        $dstArr = new ArrayPath();
198 3
        $dstArr->setArray($dest);
199
200 3
        if (!file_exists($this->fullPath($dstArr->getArrayDirectory()))) {
201 1
            return false;
202
        }
203
204 2
        if (file_exists($dst)) {
205 1
            return false;
206
        }
207
208
        try {
209
            // original call with doomed sub-calls chmod and chown - see
210
            // @link https://www.php.net/manual/en/function.rename.php#117590
211
            // return @rename($src, $dst);
212
213
            // to avoid that internal bug...
214 1
            $v1 = $this->copyDir($source, $dest);
215 1
            $v2 = $this->deleteDir($source, true);
216 1
            return $v1 && $v2;
217
            // @codeCoverageIgnoreStart
218
        } catch (Throwable $ex) {
219
            throw new FilesException($this->getFlLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex);
220
        }
221
        // @codeCoverageIgnoreEnd
222
    }
223
224 6
    public function deleteDir(array $entry, bool $deep = false): bool
225
    {
226 6
        $path = $this->fullPath($entry);
227
        try {
228 6
            if ($deep) {
229 3
                return $this->removeCycle($path);
230
            } else {
231 6
                return @rmdir($path);
232
            }
233
            // @codeCoverageIgnoreStart
234
        } catch (Throwable $ex) {
235
            throw new FilesException($this->getFlLang()->flCannotRemoveDir($path), $ex->getCode(), $ex);
236
        }
237
        // @codeCoverageIgnoreEnd
238
    }
239
240
    /**
241
     * @param array<string> $path
242
     * @throws PathsException
243
     * @return string
244
     */
245 19
    protected function fullPath(array $path): string
246
    {
247 19
        return $this->getPath() . DIRECTORY_SEPARATOR . $this->compactName($path);
248
    }
249
250
    /**
251
     * @return string
252
     * @codeCoverageIgnore only when path fails
253
     */
254
    protected function noDirectoryDelimiterSet(): string
255
    {
256
        return $this->getFlLang()->flNoDirectoryDelimiterSet();
257
    }
258
}
259