GetSatStatusExtractor::fromXmlDocument()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Helpers;
6
7
use DOMDocument;
8
use PhpCfdi\CfdiExpresiones\DiscoverExtractor;
9
use PhpCfdi\CfdiExpresiones\Exceptions\UnmatchedDocumentException;
10
use PhpCfdi\CfdiExpresiones\Extractors\Comprobante32;
11
use PhpCfdi\CfdiExpresiones\Extractors\Comprobante33;
12
use PhpCfdi\CfdiExpresiones\Extractors\Comprobante40;
13
use PhpCfdi\Finkok\Services\Cancel\GetSatStatusCommand;
14
use RuntimeException;
15
16
/**
17
 * Based on a XML string or a XML Document it can extract the appropiate values to build a GetSatStatusCommand object
18
 * It is using the CFDI QR expressions
19
 */
20
class GetSatStatusExtractor
21
{
22
    /** @var string[] */
23
    private $expressionData;
24
25
    /**
26
     * GetSatStatusExtractor constructor.
27
     *
28
     * @param array<string, string> $expressionData
29
     */
30 4
    public function __construct(array $expressionData)
31
    {
32 4
        $this->expressionData = [
33 4
            're' => strval($expressionData['re'] ?? ''),
34 4
            'rr' => strval($expressionData['rr'] ?? ''),
35 4
            'tt' => strval($expressionData['tt'] ?? ''),
36 4
            'id' => strval($expressionData['id'] ?? ''),
37 4
        ];
38
    }
39
40 4
    public static function fromXmlDocument(DOMDocument $document): self
41
    {
42 4
        $discoverer = new DiscoverExtractor(
43 4
            new Comprobante40(),
44 4
            new Comprobante33(),
45 4
            new Comprobante32(),
46 4
        );
47
        try {
48 4
            $values = $discoverer->obtain($document);
49 1
        } catch (UnmatchedDocumentException $exception) {
50 1
            $message = 'Unable to obtain the expression values, document must be valid a CFDI version 4.0, 3.3 or 3.2';
51 1
            throw new RuntimeException($message, 0, $exception);
52
        }
53 3
        return new self($values);
54
    }
55
56 4
    public static function fromXmlString(string $xmlCfdi): self
57
    {
58 4
        $document = new DOMDocument();
59 4
        $document->loadXML($xmlCfdi);
60 4
        return static::fromXmlDocument($document);
61
    }
62
63 4
    public function buildCommand(): GetSatStatusCommand
64
    {
65 4
        $expData = $this->expressionData;
66 4
        return new GetSatStatusCommand($expData['re'], $expData['rr'], $expData['id'], $expData['tt']);
67
    }
68
}
69