Completed
Push — master ( 86437d...3559e6 )
by diego
08:58
created

Csv::parseResource()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
namespace HDNET\Importr\Service\Resources;
5
6
use HDNET\Importr\Domain\Model\Strategy;
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
9
/**
10
 * Description of Csv
11
 *
12
 * Tx_Importr_Service_Resources_Csv:
13
 *  [length: 1000]
14
 *  [delimiter: ,]
15
 *  [enclosure: "]
16
 *  [escape: \]
17
 *
18
 * @author timlochmueller
19
 */
20
class Csv extends AbstractResource implements ResourceInterface
21
{
22
23
    /**
24
     * @var string
25
     */
26
    protected $filepathExpression = '/.csv$/';
27
28
    /**
29
     * @var array
30
     */
31
    protected $content = [];
32
33
    /**
34
     * @var string
35
     */
36
    protected $filepath;
37
38
    /**
39
     * @return mixed
40
     */
41 2
    public function getConfiguration()
42
    {
43 2
        $configuration = parent::getConfiguration();
44 2
        $configuration['length'] = (isset($configuration['length']) && \is_numeric($configuration['length'])) ? $configuration['length'] : 1000;
45 2
        $configuration['delimiter'] = isset($configuration['delimiter']) ? $configuration['delimiter'] : ';';
46 2
        $configuration['enclosure'] = isset($configuration['enclosure']) ? $configuration['enclosure'] : '"';
47 2
        $configuration['escape'] = isset($configuration['escape']) ? $configuration['escape'] : '\\';
48 2
        $configuration['skipRows'] = isset($configuration['skipRows']) ? $configuration['skipRows'] : '0';
49 2
        return $configuration;
50
    }
51
52
    /**
53
     * @param Strategy $strategy
54
     * @param string   $filepath
55
     */
56 2
    public function start(Strategy $strategy, $filepath)
57
    {
58 2
        $this->filepath = $filepath;
59 2
    }
60
61
    /**
62
     * @return string
63
     */
64 1
    public function getFilepathExpression()
65
    {
66 1
        return $this->filepathExpression;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getFilePath()
73
    {
74
        return GeneralUtility::getFileAbsFileName($this->filepath);
75
    }
76
77 2
    public function parseResource()
78
    {
79 2
        $configuration = $this->getConfiguration();
80 2
        \ini_set('auto_detect_line_endings', '1');
81 2
        if (($handle = \fopen($this->getFilePath(), 'r')) !== false) {
82 2
            $row = 0;
83 2
            while (($buffer = $this->fgetcsv($handle, $configuration)) !== false) {
84 2
                if ($row < $configuration['skipRows']) {
85 1
                    $row++;
86 1
                    continue;
87
                }
88
89 2
                $this->content[] = $buffer;
90 2
                $row++;
91
            }
92 2
            \fclose($handle);
93
        }
94 2
        \ini_set('auto_detect_line_endings', '0');
95 2
    }
96
97
    /**
98
     * @param $handle
99
     * @param array  $configuration
100
     * @return array
101
     */
102 2
    protected function fgetcsv($handle, array $configuration)
103
    {
104 2
        return \fgetcsv($handle, $configuration['length'], $configuration['delimiter'], $configuration['enclosure'], $configuration['escape']);
105
    }
106
107
    /**
108
     * @return int
109
     */
110 2
    public function getAmount()
111
    {
112 2
        return \count($this->content);
113
    }
114
115
    /**
116
     * @param int $pointer
117
     *
118
     * @return mixed
119
     */
120 2
    public function getEntry($pointer)
121
    {
122 2
        return $this->content[$pointer];
123
    }
124
125
    public function end()
126
    {
127
    }
128
}
129