Completed
Push — master ( 0a5559...4dd4d6 )
by Giancarlos
04:06
created

FeFactory::setWsParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

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