1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Administrador |
5
|
|
|
* Date: 20/07/2017 |
6
|
|
|
* Time: 04:06 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenter\Factory; |
10
|
|
|
|
11
|
|
|
use Greenter\Builder\BuilderInterface; |
12
|
|
|
use Greenter\Model\DocumentInterface; |
13
|
|
|
use Greenter\Model\Response\BaseResult; |
14
|
|
|
use Greenter\Services\SenderInterface; |
15
|
|
|
use Greenter\XMLSecLibs\Sunat\AdapterInterface; |
16
|
|
|
use Greenter\XMLSecLibs\Sunat\SunatXmlSecAdapter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class FeFactory |
20
|
|
|
* @package Greenter\Factory |
21
|
|
|
*/ |
22
|
|
|
class FeFactory implements FactoryInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AdapterInterface |
26
|
|
|
*/ |
27
|
|
|
private $signer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sender service. |
31
|
|
|
* |
32
|
|
|
* @var SenderInterface |
33
|
|
|
*/ |
34
|
|
|
private $sender; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Ultimo xml generado. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $lastXml; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Xml Builder. |
45
|
|
|
* |
46
|
|
|
* @var BuilderInterface |
47
|
|
|
*/ |
48
|
|
|
private $builder; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* BaseFactory constructor. |
52
|
|
|
*/ |
53
|
82 |
|
public function __construct() |
54
|
|
|
{ |
55
|
82 |
|
$this->signer = new SunatXmlSecAdapter(); |
56
|
82 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get document builder. |
60
|
|
|
* |
61
|
|
|
* @return BuilderInterface |
62
|
|
|
*/ |
63
|
|
|
public function getBuilder() |
64
|
|
|
{ |
65
|
|
|
return $this->builder; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get sender service. |
70
|
|
|
* |
71
|
|
|
* @return SenderInterface |
72
|
|
|
*/ |
73
|
|
|
public function getSender() |
74
|
|
|
{ |
75
|
|
|
return $this->sender; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set sender service. |
80
|
|
|
* |
81
|
|
|
* @param SenderInterface $sender |
82
|
|
|
* @return FeFactory |
83
|
|
|
*/ |
84
|
58 |
|
public function setSender($sender) |
85
|
|
|
{ |
86
|
58 |
|
$this->sender = $sender; |
87
|
58 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set document builder. |
92
|
|
|
* |
93
|
|
|
* @param BuilderInterface $builder |
94
|
|
|
* @return FeFactory |
95
|
|
|
*/ |
96
|
72 |
|
public function setBuilder($builder) |
97
|
|
|
{ |
98
|
72 |
|
$this->builder = $builder; |
99
|
72 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return AdapterInterface |
104
|
|
|
*/ |
105
|
|
|
public function getSigner() |
106
|
|
|
{ |
107
|
|
|
return $this->signer; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param AdapterInterface $signer |
112
|
|
|
* @return FeFactory |
113
|
|
|
*/ |
114
|
82 |
|
public function setSigner($signer) |
115
|
|
|
{ |
116
|
82 |
|
$this->signer = $signer; |
117
|
82 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Build and send document. |
122
|
|
|
* |
123
|
|
|
* @param DocumentInterface $document |
124
|
|
|
* @return BaseResult |
125
|
|
|
*/ |
126
|
58 |
|
public function send(DocumentInterface $document) |
127
|
|
|
{ |
128
|
58 |
|
$this->lastXml = $this->getXmmlSigned($document); |
129
|
|
|
|
130
|
58 |
|
return $this->sender->send($document->getName(), $this->lastXml); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get Last XML Signed. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
12 |
|
public function getLastXml() |
139
|
|
|
{ |
140
|
12 |
|
return $this->lastXml; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param DocumentInterface $document |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
72 |
|
public function getXmmlSigned(DocumentInterface $document) |
148
|
|
|
{ |
149
|
72 |
|
$xml = $this->builder->build($document); |
150
|
|
|
|
151
|
72 |
|
return $this->signer->signXml($xml); |
152
|
|
|
} |
153
|
|
|
} |