Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

FileParser::getRowsFromCsv()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\FieldNotes\Import;
4
5
use Oc\FieldNotes\Exception\FileFormatException;
6
use Symfony\Component\HttpFoundation\File\File;
7
8
/**
9
 * Class FileParser
10
 *
11
 * @package Oc\FieldNotes\Import
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
12
 */
13
class FileParser
14
{
15
    /**
16
     * @var StructMapper
0 ignored issues
show
introduced by
StructMapper => \Array\StructMapper
Loading history...
17
     */
18
    private $structMapper;
19
20
    /**
21
     * FileParser constructor.
22
     *
23
     * @param StructMapper $structMapper
0 ignored issues
show
introduced by
StructMapper => \Array\StructMapper
Loading history...
24
     */
25
    public function __construct(StructMapper $structMapper)
26
    {
27
        $this->structMapper = $structMapper;
28
    }
29
30
    /**
31
     * Parses the given file
32
     *
33
     * @param File $file
0 ignored issues
show
introduced by
File => \Symfony\Component\HttpFoundation\File\File
Loading history...
34
     *
35
     * @return array
36
     *
37
     * @throws FileFormatException
0 ignored issues
show
introduced by
FileFormatException => \Oc\FieldNotes\Exception\FileFormatException
Loading history...
38
     */
39
    public function parseFile(File $file)
40
    {
41
        $content = $this->getSanitizedFileContent($file);
42
43
        $rows = $this->getRowsFromCsv($content);
44
45
        return $this->structMapper->map($rows);
46
    }
47
48
    /**
49
     * Fetches the content of the file and returns it.
50
     *
51
     * @param File $file
0 ignored issues
show
introduced by
File => \Symfony\Component\HttpFoundation\File\File
Loading history...
52
     *
53
     * @return string
54
     */
55
    private function getSanitizedFileContent(File $file)
56
    {
57
        $content = file_get_contents($file->getRealPath());
58
        $content = str_replace("\xFF\xFE", '', $content);
59
        $content = mb_convert_encoding($content, 'UTF-8', 'UTF-16LE');
60
        $content = str_replace("\r\n", "\n", $content);
61
62
        return $content;
63
    }
64
65
    /**
66
     * Fetches rows from csv content.
67
     *
68
     * @param string $content
69
     *
70
     * @return array
71
     *
72
     * @throws FileFormatException
0 ignored issues
show
introduced by
FileFormatException => \Oc\FieldNotes\Exception\FileFormatException
Loading history...
73
     */
74
    private function getRowsFromCsv($content)
75
    {
76
        $lines = explode("\n", $content);
77
        $lines = array_filter($lines);
78
79
        $rows = [];
80
81
        foreach ($lines as $line) {
82
            $row = str_getcsv($line);
83
            $row = array_map('trim', $row);
84
85
            if (count($row) !== 4) {
86
                throw new FileFormatException('A row contains more or less than 4 columns');
87
            }
88
89
            $rows[] = $row;
90
        }
91
92
        return $rows;
93
    }
94
}
95