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

CsvExtractor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeCsvLine() 0 3 1
B getTraversable() 0 22 6
A __construct() 0 4 1
A getFirstRecord() 0 16 4
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\Extractors\File\FileExtractorAbstract;
15
use fab2s\YaEtl\Traits\CsvHandlerTrait;
16
17
/**
18
 * Class CsvExtractor
19
 */
20
class CsvExtractor extends FileExtractorAbstract
21
{
22
    use CsvHandlerTrait;
23
24
    /**
25
     * CsvLoader constructor.
26
     *
27
     * @param resource|string $input
28
     * @param string|null     $delimiter
29
     * @param string|null     $enclosure
30
     * @param string|null     $escape
31
     *
32
     * @throws NodalFlowException
33
     * @throws YaEtlException
34
     */
35
    public function __construct($input, $delimiter = null, $enclosure = null, $escape = null)
36
    {
37
        parent::__construct($input);
38
        $this->initCsvOptions($delimiter, $enclosure, $escape);
39
    }
40
41
    /**
42
     * @param mixed $param
43
     *
44
     * @return \Generator
45
     */
46
    public function getTraversable($param = null)
47
    {
48
        if (!$this->extract($param) || false === ($firstRecord = $this->getFirstRecord())) {
49
            return;
50
        }
51
52
        $firstRecord = str_getcsv($firstRecord, $this->delimiter, $this->enclosure, $this->escape);
53
        if ($this->useHeader) {
54
            $this->header = $firstRecord;
55
        } else {
56
            yield $firstRecord;
57
        }
58
59
        while (false !== ($record = fgetcsv($this->handle, 0, $this->delimiter, $this->enclosure, $this->escape))) {
60
            if ($this->useHeader) {
61
                $record = array_combine($this->header, $record);
0 ignored issues
show
Bug introduced by
It seems like $this->header can also be of type null; however, parameter $keys of array_combine() does only seem to accept array, 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

61
                $record = array_combine(/** @scrutinizer ignore-type */ $this->header, $record);
Loading history...
62
            }
63
64
            yield $record;
65
        }
66
67
        $this->releaseHandle();
68
    }
69
70
    /**
71
     * @return string|false
72
     */
73
    protected function getFirstRecord()
74
    {
75
        while (false !== ($line = fgets($this->handle))) {
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fgets() 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

75
        while (false !== ($line = fgets(/** @scrutinizer ignore-type */ $this->handle))) {
Loading history...
76
            if ($line = $this->trimBom(trim($line))) {
77
                // obey excel sep
78
                if (strpos($line, 'sep=') === 0) {
79
                    $this->useSep    = true;
80
                    $this->delimiter = $line[4];
81
                    continue;
82
                }
83
84
                return $line;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param array $record
93
     *
94
     * @return bool|int
95
     */
96
    protected function writeCsvLine(array $record)
97
    {
98
        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

98
        return fputcsv(/** @scrutinizer ignore-type */ $this->handle, $record, $this->delimiter, $this->enclosure, $this->escape);
Loading history...
99
    }
100
}
101