CsvEntityExtractor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 44
ccs 20
cts 20
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileExtension() 0 3 1
A extractFileContent() 0 29 5
1
<?php
2
3
namespace Smart\EtlBundle\Extractor;
4
5
/**
6
 * Nicolas Bastien <[email protected]>
7
 */
8
class CsvEntityExtractor extends AbstractFolderExtrator implements ExtractorInterface
9
{
10
    use EntityFileExtractorTrait;
11
12
    /**
13
     * @inheritDoc
14
     */
15 5
    protected function getFileExtension()
16
    {
17 5
        return 'csv';
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23 5
    protected function extractFileContent($filepath)
24
    {
25 5
        $file = new \SplFileObject($filepath);
26
27
        //csv file have to be formatted with headers on the first line
28 5
        $headers = $file->fgetcsv();
29 5
        $nbHeaders = count($headers);
30
31 5
        $datas = [];
32 5
        while (!$file->eof()) {
33 4
            $csvData = $file->fgetcsv();
34 4
            if (count($csvData) != $nbHeaders) {
35 2
                continue;
36
            }
37
            //Handle many relations
38 4
            $csvData = array_map(function ($e) {
39 4
                if (strpos($e, '|') !== false) {
40 2
                    if (strlen($e) == 1) {
41 2
                        return [];
42
                    }
43 2
                    return explode('|', $e);
44
                }
45 4
                return $e;
46 4
            }, $csvData);
47
48 4
            $datas[] = array_combine($headers, $csvData);
49
        }
50
51 5
        return $datas;
52
    }
53
}
54