Completed
Push — master ( 494bdc...a919b1 )
by Sébastien
13:15
created

DirectoryScanner::getFinder()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Cli\FileSystem;
6
7
use Symfony\Component\Finder\Finder;
8
9
class DirectoryScanner
10
{
11 4
    public function getFinder(string $path, ?array $validExtensions = null): Finder
12
    {
13 4
        $finder = (new Finder())->files()
14 4
            ->in($path)
15 4
            ->ignoreUnreadableDirs();
16 4
        if ($validExtensions !== null && count($validExtensions) > 0) {
17 4
            $finder->name(sprintf(
18 4
                '/\.(%s)$/i',
19 4
                implode('|', $validExtensions)
20
            ));
21
        }
22
23 4
        return $finder;
24
    }
25
26
    /**
27
     * @return array<\SplFileInfo>
28
     */
29 4
    public function findFiles(string $path, ?array $validExtensions = null, bool $recursive = true): array
30
    {
31 4
        $finder = $this->getFinder($path, $validExtensions);
32
33 4
        if (!$recursive) {
34 2
            $finder->depth('== 0');
35
        }
36
37 4
        $videos = [];
38
39
        /** @var \SplFileInfo $file */
40 4
        foreach ($finder as $file) {
41 4
            $videos[] = $file;
42
        }
43
44 4
        return $videos;
45
    }
46
}
47