CsvParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Fmaj\LaposteDatanovaBundle\Parser;
4
5
use Fmaj\LaposteDatanovaBundle\Service\Finder;
6
7
class CsvParser implements ParserInterface
8
{
9
    /** File format */
10
    const FORMAT = 'csv';
11
12
    /** CSV delimiter */
13
    const DELIMITER = ';';
14
15
    /**
16
     * @var Finder
17
     */
18
    private $finder;
19
20
    /**
21
     * @param Finder $finder
22
     */
23 1
    public function __construct(Finder $finder)
24
    {
25 1
        $this->finder = $finder;
26 1
    }
27
28
    /**
29
     * @param string $dataset
30
     * @param string $delimiter
31
     *
32
     * @return false|array
33
     */
34 1
    public function parse($dataset, $delimiter = self::DELIMITER)
35
    {
36 1
        $data = false;
37 1
        $path = $this->finder->findDataset($dataset, self::FORMAT);
38 1
        if (false !== $path) {
39 1
            $data = array();
40 1
            $lines = file($path);
41 1
            $columns = str_getcsv(strtolower($lines[0]), $delimiter);
42 1
            unset($lines[0]);
43 1
            foreach ($lines as $line) {
44 1
                $lineValues = str_getcsv($line, $delimiter);
45 1
                $lineData = array();
46 1
                foreach ($lineValues as $key => $value) {
47 1
                    $lineData[$columns[$key]] = $value;
48 1
                }
49 1
                $data[] = $lineData;
50 1
            }
51 1
        }
52
53 1
        return $data;
54
    }
55
}
56