Completed
Push — master ( e1d569...050b2a )
by Giancarlos
05:11
created

FeFactory::setWsParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3.0987
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\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
/**
27
 * Class FeFactory
28
 * @package Greenter\Factory
29
 */
30
class FeFactory implements FeFactoryInterface
31
{
32
    /**
33
     * @var FeBuilderInteface
34
     */
35
    private $builder;
36
37
    /**
38
     * @var SignedXml
39
     */
40
    private $signer;
41
42
    /**
43
     * @var ZipFactory
44
     */
45
    private $zipper;
46
47
    /**
48
     * @var WsSunatInterface
49
     */
50
    private $sender;
51
52
    /**
53
     * Ultimo xml generado.
54
     *
55
     * @var string
56
     */
57
    private $lastXml;
58
    /**
59
     * @var Company
60
     */
61
    private $company;
62
63
    /**
64
     * FeFactory constructor.
65
     */
66 32
    public function __construct()
67
    {
68 32
        $this->builder = new FeBuilder();
69 32
        $this->signer = new SignedXml();
70 32
        $this->sender = new FeSunat();
71 32
        $this->zipper = new ZipFactory();
72 32
    }
73
74
    /**
75
     * @param Invoice $invoice
76
     * @return BillResult
77
     */
78 6
    public function sendInvoice(Invoice $invoice)
79
    {
80 6
        $xml = $this->builder->buildInvoice($invoice);
81 4
        $filename = $invoice->getFilename($this->company->getRuc());
82
83 4
        return $this->getBillResult($xml, $filename);
84
    }
85
86
    /**
87
     * Envia una Nota de Credito o Debito.
88
     *
89
     * @param Note $note
90
     * @return BillResult
91
     */
92 12
    public function sendNote(Note $note)
93
    {
94 12
        $xml = $this->builder->buildNote($note);
95 8
        $filename = $note->getFilename($this->company->getRuc());
96
97 8
        return $this->getBillResult($xml, $filename);
98
    }
99
100
    /**
101
     * Envia un resumen diario de Boletas.
102
     *
103
     * @param Summary $summary
104
     * @return SummaryResult
105
     */
106 6
    public function sendResumen(Summary $summary)
107
    {
108 6
        $xml = $this->builder->buildSummary($summary);
109 4
        $filename = $summary->getFileName($this->company->getRuc());
110
111 4
        return $this->getSummaryResult($xml, $filename);
112
    }
113
114
    /**
115
     * Envia una comunicacion de Baja.
116
     *
117
     * @param Voided $voided
118
     * @return SummaryResult
119
     */
120 6
    public function sendBaja(Voided $voided)
121
    {
122 6
        $xml = $this->builder->buildVoided($voided);
123 4
        $filename = $voided->getFileName($this->company->getRuc());
124
125 4
        return $this->getSummaryResult($xml, $filename);
126
    }
127
128
    /**
129
     * Get Status by Ticket.
130
     *
131
     * @param string $ticket
132
     * @return StatusResult
133
     */
134 2
    public function getStatus($ticket)
135
    {
136 2
        return $this->sender->getStatus($ticket);
137
    }
138
139
    /**
140
     * @param $company
141
     * @return $this
142
     */
143 32
    public function setCompany(Company $company)
144
    {
145 32
        $this->company = $company;
146 32
        $this->builder->setCompany($company);
147
148 32
        return $this;
149
    }
150
151
    /**
152
     * @param array $params
153
     */
154 32
    public function setParameters($params)
155
    {
156 32
        $this->setWsParams($params['ws']);
157
158 32
        if (isset($params['xml'])) {
159 32
            $this->builder->setParameters($params['xml']);
160 32
        }
161
162 32
        if (isset($params['cert'])) {
163 32
            $this->signer->setCertificate($params['cert']);
164 32
        }
165 32
    }
166
167
    /**
168
     * Get Last XML Signed.
169
     *
170
     * @return string
171
     */
172 10
    public function getLastXml()
173
    {
174 10
        return $this->lastXml;
175
    }
176
177
    /**
178
     * @param array $ws
179
     */
180 32
    private function setWsParams($ws)
181
    {
182 32
        $this->sender->setCredentials($ws['user'], $ws['pass']);
183 32
        if (isset($ws['service'])) {
184 32
            $this->sender->setService($ws['service']);
185 32
        }
186 32
        if (isset($ws['wsdl'])) {
187
            $this->sender->setUrlWsdl($ws['wsdl']);
188
        }
189 32
    }
190
191
    /**
192
     * @param string $xml
193
     * @param string $filename
194
     * @return BillResult
195
     */
196 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...
197
    {
198 12
        $this->lastXml = $this->getXmmlSigned($xml);
199
200 12
        $zip = $this->zipper->compress("$filename.xml", $this->lastXml);
201 12
        return $this->sender->send("$filename.zip", $zip);
202
    }
203
204
    /**
205
     * @param string $xml
206
     * @param string $filename
207
     * @return SummaryResult
208
     */
209 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...
210
    {
211 8
        $this->lastXml = $this->getXmmlSigned($xml);
212
213 8
        $zip = $this->zipper->compress("$filename.xml", $this->lastXml);
214 8
        return $this->sender->sendSummary("$filename.zip", $zip);
215
    }
216
217
    /**
218
     * @param string $xml
219
     * @return string
220
     */
221 20
    private function getXmmlSigned($xml)
222
    {
223 20
        return $this->signer->sign($xml);
224
    }
225
}