Completed
Push — master ( a35e81...521884 )
by Carlos C
03:49 queued 10s
created

ConvertScript::createXmlResolver()   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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\CfdiToPdf\Script;
6
7
use CfdiUtils\Cleaner\Cleaner;
8
use CfdiUtils\Nodes\XmlNodeUtils;
9
use CfdiUtils\XmlResolver\XmlResolver;
10
use PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder;
11
use PhpCfdi\CfdiToPdf\CfdiDataBuilder;
12
use PhpCfdi\CfdiToPdf\Converter;
13
use RuntimeException;
14
15
class ConvertScript
16
{
17 1
    public function run(ConvertOptions $options)
18
    {
19 1
        $source = $this->openSource($options->inputFile(), $options->doCleanInput());
20
21 1
        $comprobante = XmlNodeUtils::nodeFromXmlString($source);
22 1
        $cfdiData = $this->createCfdiDataBuilder()
23 1
            ->withXmlResolver($this->createXmlResolver($options->resolverLocation()))
24 1
            ->build($comprobante);
25
26 1
        $converter = $this->defaultConverter();
27 1
        $converter->createPdfAs($cfdiData, $options->outputFile());
28 1
    }
29
30 6
    public function openSource(string $inputfile, bool $doCleanInput): string
31
    {
32 6
        if ('' === $inputfile) {
33 1
            throw new RuntimeException('Did not provide an input file');
34
        }
35 5
        $filename = (string) realpath($inputfile);
36 5
        if ('' === $filename) {
37 1
            throw new RuntimeException("The file $inputfile does not exists");
38
        }
39 4
        if (! is_file($filename)) {
40 1
            throw new RuntimeException("The path $inputfile is not a file");
41
        }
42
43
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
44 3
        $source = strval(@file_get_contents($filename));
45 3
        if ('' === $source) {
46 1
            throw new RuntimeException("The file $inputfile is empty");
47
        }
48 2
        if ($doCleanInput) {
49 2
            $source = $this->cleanSource($source);
50
        }
51
52 2
        return $source;
53
    }
54
55 1
    public function cleanSource(string $source): string
56
    {
57 1
        return Cleaner::staticClean($source);
58
    }
59
60 1
    public function createXmlResolver(string $resolverLocation): XmlResolver
61
    {
62 1
        return new XmlResolver($resolverLocation);
63
    }
64
65 1
    public function createCfdiDataBuilder(): CfdiDataBuilder
66
    {
67 1
        return new CfdiDataBuilder();
68
    }
69
70 1
    public function defaultConverter(): Converter
71
    {
72 1
        return new Converter(new Html2PdfBuilder());
73
    }
74
}
75