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