Completed
Push — develop ( 9f140d...7abee3 )
by Arkadiusz
02:42
created

FilesDataset   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A scanRootPath() 0 6 2
A scanDir() 0 9 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Dataset;
6
7
use Phpml\Exception\DatasetException;
8
9
class FilesDataset extends ArrayDataset
10
{
11
    /**
12
     * @param string $rootPath
13
     *
14
     * @throws DatasetException
15
     */
16
    public function __construct(string $rootPath)
17
    {
18
        if (!is_dir($rootPath)) {
19
            throw DatasetException::missingFolder($rootPath);
20
        }
21
22
        $this->scanRootPath($rootPath);
23
    }
24
25
    /**
26
     * @param string $rootPath
27
     */
28
    private function scanRootPath(string $rootPath)
29
    {
30
        foreach (glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir) {
31
            $this->scanDir($dir);
32
        }
33
    }
34
35
    /**
36
     * @param string $dir
37
     */
38
    private function scanDir(string $dir)
39
    {
40
        $target = basename($dir);
41
42
        foreach (array_filter(glob($dir.DIRECTORY_SEPARATOR.'*'), 'is_file') as $file) {
43
            $this->samples[] = [file_get_contents($file)];
44
            $this->targets[] = $target;
45
        }
46
    }
47
}
48