Passed
Push — master ( 0c1223...63e4c8 )
by Fabrice
02:56
created

CsvLoader::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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\Events\FlowEventInterface;
13
use fab2s\NodalFlow\NodalFlowException;
14
use fab2s\NodalFlow\YaEtlException;
15
use fab2s\YaEtl\Traits\CsvHandlerTrait;
16
use fab2s\YaEtl\YaEtl;
17
18
/**
19
 * Class CsvLoader
20
 */
21
class CsvLoader extends FileLoaderAbstract
22
{
23
    use CsvHandlerTrait;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $isFirstLine = true;
29
30
    /**
31
     * CsvLoader constructor.
32
     *
33
     * @param string $destination
34
     * @param string $delimiter
35
     * @param string $enclosure
36
     * @param string $escape
37
     *
38
     * @throws NodalFlowException
39
     * @throws YaEtlException
40
     */
41
    public function __construct($destination, $delimiter = ',', $enclosure = '"', $escape = '\\')
42
    {
43
        parent::__construct($destination);
44
        $this->delimiter = $delimiter;
45
        $this->enclosure = $enclosure;
46
        $this->escape    = $escape;
47
48
        /* @var YaEtl $carrier */
49
        /*$carrier = $this->getCarrier();
50
        $carrier->getDispatcher()
51
            ->addListener(FlowEvent::FLOW_START, function(FlowEventInterface $event) {
52
            $yaEtl = $event->getFlow();
53
            // do stuff ...
54
        });*/
55
    }
56
57
    /**
58
     * Execute this Node
59
     *
60
     * @param array $param
61
     */
62
    public function exec($param)
63
    {
64
        $this->handleFirstLine($param)
65
            ->writeCsvLine($param);
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function writeSep()
72
    {
73
        if ($this->useSep) {
74
            fwrite($this->handle, "sep=$this->delimiter" . PHP_EOL);
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param array $param
82
     *
83
     * @return $this
84
     */
85
    public function writeHeader(array $param)
86
    {
87
        if ($this->useHeader) {
88
            if (!isset($this->header)) {
89
                $this->header = array_keys($param);
90
            }
91
92
            $this->writeCsvLine($this->header);
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param array $record
100
     *
101
     * @return bool|int
102
     */
103
    public function writeCsvLine(array $record)
104
    {
105
        return fputcsv($this->handle, $record, $this->delimiter, $this->enclosure, $this->escape);
106
    }
107
108
    /**
109
     * @param array $param
110
     *
111
     * @return $this
112
     */
113
    protected function handleFirstLine($param)
114
    {
115
        if ($this->isFirstLine) {
116
            $this->writeBom()
117
                ->writeSep()
118
                ->writeHeader($param);
119
            $this->isFirstLine = false;
120
        }
121
122
        return $this;
123
    }
124
}
125