CsvLoader::processFile()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
ccs 16
cts 16
cp 1
crap 5
rs 9.4555
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