CsvEntityExtractor::extractFileContent()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 29
ccs 18
cts 18
cp 1
crap 5
rs 9.3888
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