Passed
Push — master ( bc2c58...de2b0e )
by Carlos C
02:23 queued 12s
created

AbstractCsvInjector::sourceFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate;
6
7
use Generator;
8
use Iterator;
9
use PhpCfdi\SatCatalogosPopulate\Database\DataTable;
10
use PhpCfdi\SatCatalogosPopulate\Database\DataTableGateway;
11
use PhpCfdi\SatCatalogosPopulate\Database\Repository;
12
use PhpCfdi\SatCatalogosPopulate\Utils\ArrayProcessors\RightTrim;
13
use PhpCfdi\SatCatalogosPopulate\Utils\CsvFile;
14
use Psr\Log\LoggerInterface;
15
use RuntimeException;
16
17
abstract class AbstractCsvInjector implements InjectorInterface
18
{
19
    abstract public function checkHeaders(CsvFile $csv): void;
20
21
    abstract public function dataTable(): DataTable;
22
23 316
    public function __construct(private string $sourceFile)
24
    {
25 316
    }
26
27 52
    public function sourceFile(): string
28
    {
29 52
        return $this->sourceFile;
30
    }
31
32 11
    public function validate(): void
33
    {
34 11
        if (! file_exists($this->sourceFile) || is_dir($this->sourceFile) || ! is_readable($this->sourceFile)) {
35 3
            throw new RuntimeException("El archivo $this->sourceFile no existe o es un directorio o no se puede leer");
36
        }
37 8
    }
38
39 9
    public function inject(Repository $repository, LoggerInterface $logger): int
40
    {
41 9
        $tableName = $this->dataTable()->name();
42 9
        $filename = basename($this->sourceFile);
43
44 9
        $gateway = new DataTableGateway($this->dataTable(), $repository);
45 9
        if ($this->shouldRecreateTable()) {
46 9
            $logger->info("Creando tabla {$tableName}...");
47 9
            $gateway->recreate();
48
        }
49
50 9
        $logger->info("Verificando encabezado de {$filename}...");
51 9
        $csv = $this->createCsvFileReader();
52 9
        $this->checkHeaders($csv);
53
54 9
        $logger->info("Inyectando contenidos de {$filename} a {$tableName}...");
55 9
        $injected = $this->injectCsvToDataTable($csv, $gateway);
56 9
        $logger->info("Se inyectaron {$injected} registros en {$tableName}");
57
58 9
        return $injected;
59
    }
60
61 6
    protected function createCsvFileReader(): CsvFile
62
    {
63 6
        return new CsvFile($this->sourceFile(), new RightTrim());
64
    }
65
66 9
    protected function injectCsvToDataTable(CsvFile $csv, DataTableGateway $gateway): int
67
    {
68 9
        $inserted = 0;
69 9
        foreach ($this->readLinesFromCsv($csv) as $line) {
70 9
            $gateway->insert(
71 9
                $gateway->dataTable()->fields()->transform($line)
72
            );
73 9
            $inserted = $inserted + 1;
74
        }
75 9
        return $inserted;
76
    }
77
78
    /**
79
     * @return Generator<int, array<int, scalar>>
80
     */
81 5
    protected function readLinesFromCsv(CsvFile $csv): Iterator
82
    {
83 5
        yield from $csv->readLines();
84 5
    }
85
86 9
    protected function shouldRecreateTable(): bool
87
    {
88 9
        return true;
89
    }
90
}
91