Passed
Push — master ( 309dcb...6acad1 )
by Petr
02:25
created

VolumeTargetFlat   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 25
rs 10
ccs 14
cts 14
cp 1
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B lookup() 0 23 7
1
<?php
2
3
namespace kalanis\kw_storage\Storage\Target;
4
5
6
use kalanis\kw_storage\Interfaces\ITargetFlat;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use SplFileInfo;
10
use Traversable;
11
12
13
/**
14
 * Class VolumeTargetFlat
15
 * @package kalanis\kw_storage\Storage\Target
16
 * Lookup deeper than into selected node/directory; use as flat structure
17
 */
18
class VolumeTargetFlat extends Volume implements ITargetFlat
19
{
20 1
    public function lookup(string $path): Traversable
21
    {
22 1
        $real = realpath($path);
23 1
        if (false === $real) {
24 1
            return;
25
        }
26
27 1
        $len = mb_strlen($real . DIRECTORY_SEPARATOR);
28 1
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($real)) as $file) {
29
            /** @var SplFileInfo $file */
30 1
            $currentPath = mb_substr(strval($file), $len);
31 1
            if ('..' == $file->getFilename()) {
32
                // parent skip
33 1
            } elseif ('.' == $file->getFilename()) {
34
                // current process
35 1
                $lastSlash = mb_strrpos($file->getRealPath(), DIRECTORY_SEPARATOR);
36 1
                if (false === $lastSlash || '.' == $currentPath) {
37 1
                    yield '';
38
                } else {
39 1
                    yield DIRECTORY_SEPARATOR . mb_substr($file->getRealPath(), $len);
40
                }
41
            } else {
42 1
                yield DIRECTORY_SEPARATOR . $currentPath;
43
            }
44
        }
45 1
    }
46
}
47