Passed
Push — refactor/backendModule-ValueOb... ( 6d59f0...584f16 )
by Tomas Norre
07:12
created

CrawlerCsvWriter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayToCsv() 0 11 2
A getRowHeaders() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace AOE\Crawler\Writer\FileWriter\CsvWriter;
5
6
/*
7
 * (c) 2020 AOE GmbH <[email protected]>
8
 *
9
 * This file is part of the TYPO3 Crawler Extension.
10
 *
11
 * It is free software; you can redistribute it and/or modify it under
12
 * the terms of the GNU General Public License, either version 2
13
 * of the License, or any later version.
14
 *
15
 * For the full copyright and license information, please read the
16
 * LICENSE.txt file that was distributed with this source code.
17
 *
18
 * The TYPO3 project - inspiring people to share!
19
 */
20
21
use TYPO3\CMS\Core\Utility\CsvUtility;
22
23
final class CrawlerCsvWriter implements CsvWriter
24
{
25
    const CARRIAGE_RETURN = 13;
26
27
    public function arrayToCsv(array $records): string
28
    {
29
        $csvLines = [];
30
        reset($records);
31
32
        $csvLines[] = $this->getRowHeaders($records);
33
        foreach ($records as $row) {
34
            $csvLines[] = CsvUtility::csvValues($row);
35
        }
36
37
        return implode(chr(self::CARRIAGE_RETURN) . chr(10), $csvLines);
38
    }
39
40
    private function getRowHeaders(array $lines): string
41
    {
42
        $fieldNames = array_keys(current($lines));
43
        return CsvUtility::csvValues($fieldNames);
44
    }
45
}
46