GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CFDI   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 156
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A add() 0 4 1
A getCadenaOriginal() 0 13 1
A getSello() 0 7 1
A putSello() 0 9 1
A getCertificado() 0 6 1
A putCertificado() 0 9 1
A xml() 0 6 1
A getXML() 0 4 1
A save() 0 4 1
A getXSL() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the cfdi-xml project.
5
 *
6
 * (c) Kinedu
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kinedu\CfdiXML;
13
14
use DOMDocument;
15
use XSLTProcessor;
16
use Kinedu\CfdiXML\Common\Node;
17
use Kinedu\CfdiXML\Node\Comprobante;
18
19
class CFDI
20
{
21
    /** @var string */
22
    const XSL_ENDPOINT = 'http://www.sat.gob.mx/sitio_internet/cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt';
23
24
    /** @var string */
25
    protected $version = '3.3';
26
27
    /** @var string */
28
    protected $key;
29
30
    /** @var string */
31
    protected $cer;
32
33
    /** @var \Kinedu\CfdiXML\Node\Comprobante */
34
    protected $comprobante;
35
36
    /**
37
     * Create a new cfdi instance.
38
     *
39
     * @param array $data
40
     * @param string $key
41
     * @param string $cer
42
     */
43
    public function __construct(array $data, string $key, string $cer)
44
    {
45
        $this->comprobante = new Comprobante($data, $this->version);
46
47
        $this->key = $key;
48
        $this->cer = $cer;
49
    }
50
51
    /**
52
     * Add new node to comprobante instance.
53
     *
54
     * @param \Kinedu\CfdiXML\Common\Node $node
55
     *
56
     * @return void
57
     */
58
    public function add(Node $node)
59
    {
60
        $this->comprobante->add($node);
61
    }
62
63
    /**
64
     * Gets the original string.
65
     *
66
     * @return string
67
     */
68
    public function getCadenaOriginal(): string
69
    {
70
        $xsl = new DOMDocument();
71
        $xsl->load($this->getXSL());
72
73
        $xslt = new XSLTProcessor();
74
        @$xslt->importStyleSheet($xsl);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
76
        $xml = new DOMDocument();
77
        $xml->loadXML($this->comprobante->getDocument()->saveXML());
78
79
        return (string) $xslt->transformToXml($xml);
80
    }
81
82
    /**
83
     * Get sello.
84
     *
85
     * @return string
86
     */
87
    public function getSello(): string
88
    {
89
        $pkey = openssl_get_privatekey($this->key);
90
        openssl_sign(@$this->getCadenaOriginal(), $signature, $pkey, OPENSSL_ALGO_SHA256);
91
        openssl_free_key($pkey);
92
        return base64_encode($signature);
93
    }
94
95
    /**
96
     * Put sello.
97
     *
98
     * @return void
99
     */
100
    protected function putSello()
101
    {
102
        $this->comprobante->setAttr(
103
            $this->comprobante->getElement(),
104
            [
105
                'Sello' => $this->getSello(),
106
            ]
107
        );
108
    }
109
110
    /**
111
     * Get certificado.
112
     *
113
     * @return string
114
     */
115
    public function getCertificado(): string
116
    {
117
        $cer = preg_replace('/(-+[^-]+-+)/', '', $this->cer);
118
        $cer = preg_replace('/\s+/', '', $cer);
119
        return $cer;
120
    }
121
122
    /**
123
     * Put certificado.
124
     *
125
     * @return void
126
     */
127
    protected function putCertificado()
128
    {
129
        $this->comprobante->setAttr(
130
            $this->comprobante->getElement(),
131
            [
132
                'Certificado' => $this->getCertificado(),
133
            ]
134
        );
135
    }
136
137
    /**
138
     * Returns the xml with the stamp and certificate attributes.
139
     *
140
     * @return DOMDocument
141
     */
142
    protected function xml(): DOMDocument
143
    {
144
        $this->putSello();
145
        $this->putCertificado();
146
        return $this->comprobante->getDocument();
147
    }
148
149
    /**
150
     * Get the xml.
151
     *
152
     * @return string
153
     */
154
    public function getXML(): string
155
    {
156
        return $this->xml()->saveXML();
157
    }
158
159
    /**
160
     * @param string $filename
161
     */
162
    public function save(string $filename)
163
    {
164
        return $this->xml()->save($filename);
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getXSL()
171
    {
172
        return static::XSL_ENDPOINT;
173
    }
174
}
175