Completed
Pull Request — master (#5)
by Carlos C
03:38
created

GetRelatedSigner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 77.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 80
ccs 27
cts 35
cp 0.7714
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sign() 0 4 1
A pacRfc() 0 3 1
A __construct() 0 6 2
A rfc() 0 3 1
A uuid() 0 3 1
A signUsingCredentials() 0 6 1
A createDocumentToSign() 0 14 3
A role() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok\Helpers;
6
7
use DOMDocument;
8
use PhpCfdi\Finkok\Definitions\RfcRole;
9
use PhpCfdi\XmlCancelacion\Credentials;
10
use PhpCfdi\XmlCancelacion\DOMSigner;
11
12
class GetRelatedSigner
13
{
14
    public const DEFAULT_PACRFC = 'CVD110412TF6';
15
16
    /** @var string */
17
    private $uuid;
18
19
    /** @var string */
20
    private $rfc;
21
22
    /** @var RfcRole */
23
    private $role;
24
25
    /** @var string */
26
    private $pacRfc;
27
28
    /**
29
     * GetRelatedSigner constructor.
30
     *
31
     * @param string $uuid
32
     * @param string $rfc
33
     * @param RfcRole|null $role If null (ommited) then uses emitter role
34
     * @param string $pacRfc If empty (ommited) then uses DEFAULT_PACRFC
35
     */
36 1
    public function __construct(string $uuid, string $rfc, RfcRole $role = null, string $pacRfc = self::DEFAULT_PACRFC)
37
    {
38 1
        $this->uuid = $uuid;
39 1
        $this->rfc = $rfc;
40 1
        $this->role = $role ?? RfcRole::emitter();
41 1
        $this->pacRfc = $pacRfc ?: static::DEFAULT_PACRFC;
42 1
    }
43
44
    public function uuid(): string
45
    {
46
        return $this->uuid;
47
    }
48
49
    public function rfc(): string
50
    {
51
        return $this->rfc;
52
    }
53
54
    public function role(): RfcRole
55
    {
56
        return $this->role;
57
    }
58
59
    public function pacRfc(): string
60
    {
61
        return $this->pacRfc;
62
    }
63
64 1
    public function createDocumentToSign(): DOMDocument
65
    {
66 1
        $document = new DOMDocument('1.0', 'UTF-8');
67 1
        $root = $document->createElementNS('http://cancelacfd.sat.gob.mx', 'PeticionConsultaRelacionados');
68 1
        $xmlns = 'http://www.w3.org/2000/xmlns/';
69 1
        $root->setAttributeNS($xmlns, 'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
70 1
        $root->setAttributeNS($xmlns, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
71 1
        $root->setAttribute('RfcEmisor', ($this->role->isEmitter()) ? $this->rfc : '');
72 1
        $root->setAttribute('RfcPacEnviaSolicitud', $this->pacRfc);
73 1
        $root->setAttribute('RfcReceptor', ($this->role->isRecipient()) ? $this->rfc : '');
74 1
        $root->setAttribute('Uuid', $this->uuid);
75 1
        $document->appendChild($root);
76 1
        $document->normalizeDocument();
77 1
        return $document;
78
    }
79
80 1
    public function sign(string $certificateFile, string $privateKeyFile, string $passPhrase): string
81
    {
82 1
        $credentials = new Credentials($certificateFile, $privateKeyFile, $passPhrase);
83 1
        return $this->signUsingCredentials($credentials);
84
    }
85
86 1
    public function signUsingCredentials(Credentials $credentials): string
87
    {
88 1
        $document = $this->createDocumentToSign();
89 1
        $domSigner = new DOMSigner($document);
90 1
        $domSigner->sign($credentials);
91 1
        return $document->saveXML();
92
    }
93
}
94