Completed
Push — master ( ae3d8f...c97ce9 )
by Giancarlos
03:44
created

FeFactory::sendBaja()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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;
10
11
use Greenter\Model\Company\Company;
12
use Greenter\Model\Response\BillResult;
13
use Greenter\Model\Response\StatusResult;
14
use Greenter\Model\Response\SummaryResult;
15
use Greenter\Model\Sale\Note;
16
use Greenter\Model\Summary\Summary;
17
use Greenter\Model\Voided\Voided;
18
use Greenter\Ws\Services\WsSunatInterface;
19
use Greenter\Xml\Builder\FeBuilderInteface;
20
use Greenter\Zip\ZipFactory;
21
use Greenter\Security\SignedXml;
22
use Greenter\Ws\Services\FeSunat;
23
use Greenter\Xml\Builder\FeBuilder;
24
use Greenter\Model\Sale\Invoice;
25
26
class FeFactory implements FeFactoryInterface
27
{
28
    /**
29
     * @var FeBuilderInteface
30
     */
31
    private $builder;
32
33
    /**
34
     * @var SignedXml
35
     */
36
    private $signer;
37
38
    /**
39
     * @var ZipFactory
40
     */
41
    private $zipper;
42
43
    /**
44
     * @var WsSunatInterface
45
     */
46
    private $sender;
47
48
    /**
49
     * Ultimo xml generado.
50
     *
51
     * @var string
52
     */
53
    private $lastXml;
54
    /**
55
     * @var Company
56
     */
57
    private $company;
58
59
    /**
60
     * FeFactory constructor.
61
     */
62 32
    public function __construct()
63
    {
64 32
        $this->builder = new FeBuilder();
65 32
        $this->signer = new SignedXml();
66 32
        $this->sender = new FeSunat();
67 32
        $this->zipper = new ZipFactory();
68 32
    }
69
70
    /**
71
     * @param Invoice $invoice
72
     * @return BillResult
73
     */
74 6
    public function sendInvoice(Invoice $invoice)
75 2
    {
76 6
        $xml = $this->builder->buildInvoice($invoice);
77 4
        $filename = $invoice->getFilename($this->company->getRuc());
78
79 4
        return $this->getBillResult($xml, $filename);
80
    }
81
82
    /**
83
     * Envia una Nota de Credito o Debito.
84
     *
85
     * @param Note $note
86
     * @return BillResult
87
     */
88 12
    public function sendNote(Note $note)
89
    {
90 12
        $xml = $this->builder->buildNote($note);
91 8
        $filename = $note->getFilename($this->company->getRuc());
92
93 8
        return $this->getBillResult($xml, $filename);
94
    }
95
96
    /**
97
     * Envia un resumen diario de Boletas.
98
     *
99
     * @param Summary $summary
100
     * @return SummaryResult
101
     */
102 6
    public function sendResumen(Summary $summary)
103
    {
104 6
        $xml = $this->builder->buildSummary($summary);
105 4
        $filename = $summary->getFileName($this->company->getRuc());
106
107 4
        return $this->getSummaryResult($xml, $filename);
108
    }
109
110
    /**
111
     * Envia una comunicacion de Baja.
112
     *
113
     * @param Voided $voided
114
     * @return SummaryResult
115
     */
116 6
    public function sendBaja(Voided $voided)
117
    {
118 6
        $xml = $this->builder->buildVoided($voided);
119 4
        $filename = $voided->getFileName($this->company->getRuc());
120
121 4
        return $this->getSummaryResult($xml, $filename);
122
    }
123
124
    /**
125
     * Get Status by Ticket.
126
     *
127
     * @param string $ticket
128
     * @return StatusResult
129
     */
130 2
    public function getStatus($ticket)
131
    {
132 2
        return $this->sender->getStatus($ticket);
133
    }
134
135
    /**
136
     * @param $company
137
     * @return $this
138
     */
139 32
    public function setCompany(Company $company)
140
    {
141 32
        $this->company = $company;
142 32
        $this->builder->setCompany($company);
143
144 32
        return $this;
145
    }
146
147
    /**
148
     * @param array $params
149
     */
150 32
    public function setParameters($params)
151
    {
152 32
        $this->setWsParams($params['ws']);
153
154 32
        if (isset($params['xml'])) {
155 32
            $this->builder->setParameters($params['xml']);
156 32
        }
157
158 32
        if (isset($params['cert'])) {
159 32
            $cert = $params['cert'];
160 32
            $this->signer->setPrivateKey($cert['private']);
161 32
            $this->signer->setPublicKey($cert['public']);
162 32
        }
163 32
    }
164
165
    /**
166
     * Get Last XML Signed.
167
     *
168
     * @return string
169
     */
170 10
    public function getLastXml()
171
    {
172 10
        return $this->lastXml;
173
    }
174
175
    /**
176
     * @param array $ws
177
     */
178 32
    private function setWsParams($ws)
179
    {
180 32
        $this->sender->setCredentials($ws['user'], $ws['pass']);
181 32
        if (isset($ws['service'])) {
182 32
            $this->sender->setService($ws['service']);
183 32
        }
184 32
        if (isset($ws['wsdl'])) {
185
            $this->sender->setUrlWsdl($ws['wsdl']);
186
        }
187 32
    }
188
189
    /**
190
     * @param string $xml
191
     * @param string $filename
192
     * @return BillResult
193
     */
194 12 View Code Duplication
    private function getBillResult($xml, $filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196 12
        $this->lastXml = $this->getXmmlSigned($xml);
197
198 12
        $zip = $this->zipper->compress("$filename.xml", $this->lastXml);
199 12
        return $this->sender->send("$filename.zip", $zip);
200
    }
201
202
    /**
203
     * @param string $xml
204
     * @param string $filename
205
     * @return SummaryResult
206
     */
207 8 View Code Duplication
    private function getSummaryResult($xml, $filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209 8
        $this->lastXml = $this->getXmmlSigned($xml);
210
211 8
        $zip = $this->zipper->compress("$filename.xml", $this->lastXml);
212 8
        return $this->sender->sendSummary("$filename.zip", $zip);
213
    }
214
215
    /**
216
     * @param string $xml
217
     * @return string
218
     */
219 20
    private function getXmmlSigned($xml)
220
    {
221 20
        return $this->signer->sign($xml);
222
    }
223
}