CsvLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processFile() 0 23 5
A load() 0 4 2
1
<?php
2
3
namespace Smart\EtlBundle\Loader;
4
5
/**
6
 * Nicolas Bastien <[email protected]>
7
 */
8
class CsvLoader extends AbstractFileLoader implements LoaderInterface
9
{
10
    /**
11
     * @inheritDoc
12
     */
13
    protected $fileExtension = 'csv';
14
15
    /**
16
     * @inheritDoc
17
     */
18 1
    public function load(array $data)
19
    {
20 1
        foreach ($data as $filename => $fileData) {
21 1
            $this->processFile($filename, $fileData);
22
        }
23 1
    }
24
25
    /**
26
     * @param string $filename
27
     * @param array $data
28
     */
29 1
    protected function processFile($filename, $data)
30
    {
31 1
        $filepath = $this->getFolderToLoad() . DIRECTORY_SEPARATOR . $filename . '.' . $this->fileExtension;
32 1
        if (!is_dir(dirname($filepath))) {
33 1
            mkdir(dirname($filepath), 0700, true);
34
        }
35 1
        $fp = fopen($filepath, 'w');
36
37
        //write headers
38 1
        fputcsv($fp, array_keys($data[0]), ',', '"');
39 1
        foreach ($data as $row) {
40 1
            $row = array_map(function ($e) {
41 1
                if (is_array($e)) {
42 1
                    if (count($e) === 0) {
43 1
                        return '|';
44
                    }
45 1
                    return implode('|', $e);
46
                }
47 1
                return $e;
48 1
            }, $row);
49 1
            fputcsv($fp, $row);
50
        }
51 1
        fclose($fp);
52 1
    }
53
}
54