Paises::dataTable()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 20
ccs 15
cts 15
cp 1
rs 9.8333
cc 3
nc 1
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Importers\Cfdi\Injectors;
6
7
use PhpCfdi\SatCatalogosPopulate\AbstractCsvInjector;
8
use PhpCfdi\SatCatalogosPopulate\Database\DataFields;
9
use PhpCfdi\SatCatalogosPopulate\Database\DataTable;
10
use PhpCfdi\SatCatalogosPopulate\Database\TextDataField;
11
use PhpCfdi\SatCatalogosPopulate\InjectorInterface;
12
use PhpCfdi\SatCatalogosPopulate\Utils\CsvFile;
13
use RuntimeException;
14
15
use function PhpCfdi\SatCatalogosPopulate\Utils\preg_is_valid;
16
17
class Paises extends AbstractCsvInjector implements InjectorInterface
18
{
19 4
    public function checkHeaders(CsvFile $csv): void
20
    {
21 4
        $csv->move(3);
22 4
        $expected = [
23 4
            'c_Pais',
24 4
            'Descripción',
25 4
            'Formato de código postal',
26 4
            'Formato de Registro de Identidad Tributaria',
27 4
            'Validación del Registro de Identidad Tributaria',
28 4
            'Agrupaciones',
29 4
        ];
30 4
        $headers = $csv->readLine();
31
32 4
        if ($expected !== $headers) {
33 1
            throw new RuntimeException("The headers did not match on file {$this->sourceFile()}");
34
        }
35
36 3
        $csv->next();
37
    }
38
39 9
    public function dataTable(): DataTable
40
    {
41 9
        $optionalPattern = function (string $input): string {
42 8
            if ('' === $input) {
43 7
                return '';
44
            }
45 6
            if (! preg_is_valid($input)) {
46 2
                throw new RuntimeException("Se ha encontrado un valor que no es un patrón: '$input'");
47
            }
48
49 4
            return $input;
50 9
        };
51
52 9
        return new DataTable('cfdi_paises', new DataFields([
53 9
            new TextDataField('id'),
54 9
            new TextDataField('texto'),
55 9
            new TextDataField('patron_codigo_postal', $optionalPattern),
56 9
            new TextDataField('patron_identidad_tributaria', $optionalPattern),
57 9
            new TextDataField('validacion_identidad_tributaria'),
58 9
            new TextDataField('agrupaciones'),
59 9
        ]));
60
    }
61
}
62