Files::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_menu\EntriesSource;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Node;
9
use kalanis\kw_menu\Interfaces\IEntriesSource;
10
use kalanis\kw_menu\MenuException;
11
use kalanis\kw_menu\Traits\TFilterHtml;
12
use kalanis\kw_paths\ArrayPath;
13
use kalanis\kw_paths\Stuff;
14
use Traversable;
15
16
17
/**
18
 * Class Files
19
 * @package kalanis\kw_menu\EntriesSource
20
 * Entries source is in passed files adapter
21
 */
22
class Files implements IEntriesSource
23
{
24
    use TFilterHtml;
25
26
    protected CompositeAdapter $files;
27
    protected ArrayPath $arrPath;
28
29 4
    public function __construct(CompositeAdapter $files)
30
    {
31 4
        $this->files = $files;
32 4
        $this->arrPath = new ArrayPath();
33 4
    }
34
35 3
    public function getFiles(array $path): Traversable
36
    {
37
        try {
38 3
            $list = $this->files->readDir($path);
39 2
            foreach ($list as $item) {
40
                /** @var Node $item */
41 2
                if (empty($item->getPath())) {
42
                    // root - skip
43 2
                    continue;
44
                }
45 2
                $this->arrPath->setArray($item->getPath());
46 2
                if ($this->filterExt(Stuff::fileExt($this->arrPath->getFileName()))) {
47 2
                    yield $this->arrPath->getFileName();
48
                }
49
            }
50 1
        } catch (FilesException $ex) {
51 1
            throw new MenuException($ex->getMessage(), $ex->getCode(), $ex);
52
        }
53 2
    }
54
}
55