Test Failed
Push — master ( dd9cb8...6d15c1 )
by Emmanuel
04:44
created

CSVWriter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 10
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2.2559
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package edyan/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Utils;
19
20
use Doctrine\DBAL\Connection;
21
use Edyan\Neuralyzer\Exception\NeuralizerException;
22
23
/**
24
 * A few generic methods to help interacting with DB
25
 */
26
class CSVWriter extends \SplFileObject
27
{
28 1
    public function __construct()
29
    {
30 1
        parent::__construct(tempnam(sys_get_temp_dir(), 'neuralyzer'), 'w');
31 1
    }
32
33 1
    public function write(array $fields)
34
    {
35 1
        $options = $this->getCsvControl();
36 1
        $delimiter = $options[0];
37 1
        $enclosure = $options[1];
38 1
        if (!empty($enclosure)) {
39 1
            return $this->fputcsv($fields, $delimiter, $enclosure);
40
        }
41
42
        $fields = array_map(function ($field) use ($delimiter, $enclosure) {
43
            return str_replace([$delimiter, PHP_EOL, chr(10)], ['', '', ''], $field);
44
        }, $fields);
45
46
        return $this->fwrite(implode($delimiter, $fields) . chr(10));
47
    }
48
}
49