Completed
Push — master ( a0d808...22db96 )
by Giancarlos
03:50
created

SignedXml   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDocXml() 0 7 1
A sign() 0 7 1
A verify() 0 7 1
A setCertificate() 0 5 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 15/07/2017
6
 * Time: 22:39
7
 */
8
9
namespace Greenter\Security;
10
11
use RobRichards\XMLSecLibs\Sunat\Adapter\SunatXmlSecAdapter;
12
13
/**
14
 * Class SignedXml
15
 * @package Greenter\Security
16
 */
17
class SignedXml
18
{
19
    /**
20
     * @var SunatXmlSecAdapter
21
     */
22
    private $adapter;
23
24
    /**
25
     * SignedXml constructor.
26
     */
27 66
    public function __construct()
28
    {
29 66
        $this->adapter = new SunatXmlSecAdapter();
30 66
    }
31
32
    /**
33
     * Firma el contenido del xml y retorna el contenido firmado.
34
     *
35
     * @param string $content
36
     * @return string
37
     */
38 42
    public function sign($content)
39
    {
40 42
        $doc = $this->getDocXml($content);
41
42 42
        $this->adapter->sign($doc);
43 42
        return $doc->saveXML();
44
    }
45
46
    /**
47
     * Verifica la firma del xml.
48
     *
49
     * @param string $content
50
     * @return bool
51
     */
52 2
    public function verify($content)
53
    {
54 2
        $doc = $this->getDocXml($content);
55 2
        $this->adapter->getPublicKey($doc);
56
57 2
        return $this->adapter->verify($doc);
58
    }
59
60
    /**
61
     * @param string $cert
62
     */
63 62
    public function setCertificate($cert)
64
    {
65 62
        $this->adapter->setPrivateKey($cert);
66 62
        $this->adapter->setPublicKey($cert);
67 62
    }
68
69
    /**
70
     * @param string $content
71
     * @return \DOMDocument
72
     */
73 42
    private function getDocXml($content)
74
    {
75 42
        $doc = new \DOMDocument();
76 42
        $doc->loadXML($content);
77
78 42
        return $doc;
79
    }
80
}