|
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\Model\DocumentInterface; |
|
12
|
|
|
use Greenter\Model\Response\BaseResult; |
|
13
|
|
|
use Greenter\Security\SignedXml; |
|
14
|
|
|
use Greenter\Validator\DocumentValidatorInterface; |
|
15
|
|
|
use Greenter\Ws\Services\SenderInterface; |
|
16
|
|
|
use Greenter\Xml\Builder\BuilderInterface; |
|
17
|
|
|
use Greenter\Xml\Exception\ValidationException; |
|
18
|
|
|
use Greenter\Zip\ZipFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class FeFactory |
|
22
|
|
|
* @package Greenter\Factory |
|
23
|
|
|
*/ |
|
24
|
|
|
class FeFactory implements FactoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SignedXml |
|
28
|
|
|
*/ |
|
29
|
|
|
private $signer; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ZipFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
private $zipper; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var SenderInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $sender; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Ultimo xml generado. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $lastXml; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var BuilderInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $builder; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var DocumentValidatorInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $validator; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $validator |
|
60
|
|
|
* @return FeFactory |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public function setValidator($validator) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$this->validator = $validator; |
|
65
|
4 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* BaseFactory constructor. |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function __construct() |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->signer = new SignedXml(); |
|
74
|
4 |
|
$this->zipper = new ZipFactory(); |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return BuilderInterface |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getBuilder() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->builder; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return SenderInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getSender() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->sender; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param SenderInterface $sender |
|
95
|
|
|
* @return FeFactory |
|
96
|
|
|
*/ |
|
97
|
4 |
|
public function setSender($sender) |
|
98
|
|
|
{ |
|
99
|
4 |
|
$this->sender = $sender; |
|
100
|
4 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param BuilderInterface $builder |
|
105
|
|
|
* @return FeFactory |
|
106
|
|
|
*/ |
|
107
|
4 |
|
public function setBuilder($builder) |
|
108
|
|
|
{ |
|
109
|
4 |
|
$this->builder = $builder; |
|
110
|
4 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param DocumentInterface $document |
|
115
|
|
|
* @return BaseResult |
|
116
|
|
|
* @throws ValidationException |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function send(DocumentInterface $document) |
|
119
|
|
|
{ |
|
120
|
4 |
|
$errs = $this->validator->validate($document); |
|
121
|
4 |
|
if (count($errs) > 0) { |
|
122
|
|
|
throw new ValidationException($errs); |
|
123
|
|
|
} |
|
124
|
4 |
|
$xml = $this->builder->build($document); |
|
125
|
4 |
|
$this->lastXml = $this->getXmmlSigned($xml); |
|
126
|
4 |
|
$filename = $document->getName(); |
|
127
|
|
|
|
|
128
|
4 |
|
$zip = $this->zipper->compress("$filename.xml", $this->lastXml); |
|
129
|
4 |
|
return $this->sender->send("$filename.zip", $zip); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set Certicated content |
|
134
|
|
|
* @param string $cert |
|
135
|
|
|
*/ |
|
136
|
4 |
|
public function setCertificate($cert) |
|
137
|
|
|
{ |
|
138
|
4 |
|
$this->signer->setCertificate($cert); |
|
139
|
4 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get Last XML Signed. |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getLastXml() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->lastXml; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $xml |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
4 |
|
private function getXmmlSigned($xml) |
|
156
|
|
|
{ |
|
157
|
4 |
|
return $this->signer->sign($xml); |
|
158
|
|
|
} |
|
159
|
|
|
} |