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