AbstractFolderExtrator::getFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Smart\EtlBundle\Extractor;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
/**
9
 * Nicolas Bastien <[email protected]>
10
 */
11
abstract class AbstractFolderExtrator extends AbstractExtractor
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $folderToExtract;
17
18
    /**
19
     * @var string
20
     */
21
    abstract protected function getFileExtension();
22
23
    /**
24
     * @param string $filepath
25
     * @return mixed
26
     */
27
    abstract protected function extractFileContent($filepath);
28
29
    /**
30
     * @return string
31
     */
32
    public function getFolderToExtract()
33
    {
34
        return $this->folderToExtract;
35
    }
36
37
    /**
38
     * @param string $folderToExtract
39
     */
40 11
    public function setFolderToExtract($folderToExtract)
41
    {
42 11
        $this->folderToExtract = $folderToExtract;
43 11
    }
44
45 9
    protected function check()
46
    {
47 9
        if (!is_dir($this->folderToExtract)) {
48
            throw new \BadMethodCallException('Invalid folder to extract : ' . $this->folderToExtract);
49
        }
50 9
    }
51
52
    /**
53
     * @param string $extension
54
     * @return array
55
     */
56 9
    protected function getFiles($extension)
57
    {
58 9
        $finder = new Finder();
59 9
        $finder->in($this->folderToExtract);
60 9
        $files = [];
61
        /* @var SplFileInfo $file */
62 9
        foreach ($finder->files()->name('*.' . $extension) as $file) {
63 9
            $files[] = substr($file->getFilename(), 0, -(strlen($extension) + 1));
64
        }
65
66 9
        return array_combine($files, $files);
67
    }
68
}
69