Passed
Push — master ( bb35d3...bc1c68 )
by Fabrice
03:52
created

CsvLoader::writeCsvLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Loaders\File;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
use fab2s\YaEtl\Traits\CsvHandlerTrait;
15
16
/**
17
 * Class CsvLoader
18
 */
19
class CsvLoader extends FileLoaderAbstract
20
{
21
    use CsvHandlerTrait;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $isFirstLine = true;
27
28
    /**
29
     * CsvLoader constructor.
30
     *
31
     * @param string      $destination
32
     * @param string|null $delimiter
33
     * @param string|null $enclosure
34
     * @param string|null $escape
35
     *
36
     * @throws NodalFlowException
37
     * @throws YaEtlException
38
     */
39
    public function __construct($destination, $delimiter = null, $enclosure = null, $escape = null)
40
    {
41
        parent::__construct($destination);
42
        $this->initCsvOptions($delimiter, $enclosure, $escape);
43
    }
44
45
    /**
46
     * Execute this Node
47
     *
48
     * @param array $param
49
     */
50
    public function exec($param)
51
    {
52
        // take care about header etc
53
        if ($this->isFirstLine) {
54
            if ($this->useSep) {
55
                fwrite($this->handle, "sep=$this->delimiter\n");
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                fwrite(/** @scrutinizer ignore-type */ $this->handle, "sep=$this->delimiter\n");
Loading history...
56
            }
57
58
            if ($this->useHeader) {
59
                if (!isset($this->header)) {
60
                    $this->header = array_keys($param);
61
                }
62
63
                $this->writeCsvLine($this->header);
64
            }
65
66
            $this->isFirstLine = false;
67
        }
68
69
        $this->writeCsvLine($param);
70
    }
71
72
    /**
73
     * @param array $record
74
     *
75
     * @return bool|int
76
     */
77
    protected function writeCsvLine(array $record)
78
    {
79
        return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        return fputcsv(/** @scrutinizer ignore-type */ $this->handle, $record, $this->delimiter, $this->enclosure, $this->escape);
Loading history...
80
    }
81
}
82