Test Failed
Push — master ( 929e08...c8409e )
by Giancarlos
03:13
created

FeFactory   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 213
Duplicated Lines 6.57 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 10
dl 14
loc 213
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendInvoice() 0 7 1
A sendNote() 0 7 1
A sendResumen() 0 7 1
A sendBaja() 0 7 1
A getStatus() 0 4 1
A setCompany() 0 7 1
B setParameters() 0 22 5
A getBillResult() 7 7 1
A getSummaryResult() 7 7 1
A getXmmlSigned() 0 4 1
A getFilename() 0 11 1
A getFilenameSummary() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\BaseSale;
16
use Greenter\Model\Sale\Note;
17
use Greenter\Model\Summary\Summary;
18
use Greenter\Model\Voided\Voided;
19
use Greenter\Ws\Services\WsSunatInterface;
20
use Greenter\Xml\Builder\FeBuilderInteface;
21
use Greenter\Zip\ZipFactory;
22
use Greenter\Security\SignedXml;
23
use Greenter\Ws\Services\FeSunat;
24
use Greenter\Xml\Builder\FeBuilder;
25
use Greenter\Model\Sale\Invoice;
26
27
class FeFactory implements FeFactoryInterface
28
{
29
    /**
30
     * @var FeBuilderInteface
31
     */
32
    private $builder;
33
34
    /**
35
     * @var SignedXml
36
     */
37
    private $signer;
38
39
    /**
40
     * @var ZipFactory
41
     */
42
    private $zipper;
43
44
    /**
45
     * @var WsSunatInterface
46
     */
47
    private $sender;
48
49
    /**
50
     * @var Company
51
     */
52
    private $company;
53
54
    /**
55
     * FeFactory constructor.
56
     */
57 10
    public function __construct()
58
    {
59 10
        $this->builder = new FeBuilder();
60 10
        $this->signer = new SignedXml();
61 10
        $this->sender = new FeSunat();
62 10
        $this->zipper = new ZipFactory();
63 10
    }
64
65
    /**
66
     * @param Invoice $invoice
67
     * @return BillResult
68
     */
69 2
    public function sendInvoice(Invoice $invoice)
70
    {
71 2
        $xml = $this->builder->buildInvoice($invoice);
72 2
        $filename = $this->getFilename($invoice);
73
74 2
        return $this->getBillResult($xml, $filename);
75 2
    }
76
77
    /**
78
     * Envia una Nota de Credito o Debito.
79
     *
80
     * @param Note $note
81
     * @return BillResult
82
     */
83 2
    public function sendNote(Note $note)
84
    {
85 2
        $xml = $this->builder->buildNote($note);
86 2
        $filename = $this->getFilename($note);
87
88 2
        return $this->getBillResult($xml, $filename);
89
    }
90
91
    /**
92
     * Envia un resumen diario de Boletas.
93
     *
94
     * @param Summary $summary
95
     * @return SummaryResult
96
     */
97 4
    public function sendResumen(Summary $summary)
98
    {
99 2
        $xml = $this->builder->buildSummary($summary);
100 4
        $filename = $this->getFilenameSummary($summary);
101
102 2
        return $this->getSummaryResult($xml, $filename);
103
    }
104
105
    /**
106
     * Envia una comunicacion de Baja.
107
     *
108
     * @param Voided $voided
109
     * @return SummaryResult
110
     */
111 2
    public function sendBaja(Voided $voided)
112
    {
113 2
        $xml = $this->builder->buildVoided($voided);
114 2
        $filename = $this->getFilenameSummary($voided);
115
116 2
        return $this->getSummaryResult($xml, $filename);
117
    }
118
119
    /**
120
     * Get Status by Ticket.
121
     *
122
     * @param string $ticket
123
     * @return StatusResult
124
     */
125 2
    public function getStatus($ticket)
126
    {
127 2
        return $this->sender->getStatus($ticket);
128
    }
129
130
    /**
131
     * @param $company
132
     * @return $this
133
     */
134 10
    public function setCompany(Company $company)
135
    {
136 10
        $this->company = $company;
137 10
        $this->builder->setCompany($company);
138
139 10
        return $this;
140
    }
141
142
    /**
143
     * @param array $params
144
     */
145 10
    public function setParameters($params)
146
    {
147 10
        $ws = $params['ws'];
148 10
        $this->sender->setCredentials($ws['user'], $ws['pass']);
149 10
        if (isset($ws['service'])) {
150 10
            $this->sender->setService($ws['service']);
151
        }
152 10
153 10
        if (isset($ws['wsdl'])) {
154 10
            $this->sender->setUrlWsdl($ws['wsdl']);
155 10
        }
156
157 10
        if (isset($params['xml'])) {
158
            $this->builder->setParameters($params['xml']);
159 10
        }
160 10
161 10
        if (isset($params['cert'])) {
162 10
            $cert = $params['cert'];
163 10
            $this->signer->setPrivateKey($cert['private']);
164
            $this->signer->setPublicKey($cert['public']);
165
        }
166
    }
167
168
    /**
169
     * @param string $xml
170 4
     * @param string $filename
171
     * @return BillResult
172 4
     */
173 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...
174 4
    {
175 4
        $xmlS = $this->getXmmlSigned($xml);
176
177
        $zip = $this->zipper->compress("$filename.xml", $xmlS);
178
        return $this->sender->send("$filename.zip", $zip);
179
    }
180
181
    /**
182
     * @param string $xml
183 4
     * @param string $filename
184
     * @return SummaryResult
185 4
     */
186 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...
187 4
    {
188 4
        $xmlS = $this->getXmmlSigned($xml);
189
190
        $zip = $this->zipper->compress("$filename.xml", $xmlS);
191
        return $this->sender->sendSummary("$filename.zip", $zip);
192
    }
193
194
    /**
195 4
     * @param string $xml
196
     * @return string
197
     */
198 4
    private function getXmmlSigned($xml)
199 4
    {
200 4
        return $this->signer->sign($xml);
201 4
    }
202 4
203
    /**
204 4
     * @param BaseSale $sale
205
     * @return string
206
     */
207
    private function getFilename(BaseSale $sale)
208
    {
209
        $parts = [
210
            $this->company->getRuc(),
211
            $sale->getTipoDoc(),
212 4
            $sale->getSerie(),
213
            $sale->getCorrelativo(),
214 4
        ];
215
216 4
        return join('-', $parts);
217 2
    }
218 2
219 4
220 2
    /**
221 2
     * @param Summary|Voided $object
222 2
     * @return string
223 4
     */
224
    private function getFilenameSummary($object)
225 4
    {
226
        $parts = [$this->company->getRuc()];
227
228
        if ($object instanceof Summary) {
229
            $parts[] = 'RC';
230
            $parts[] = $object->getFecResumen()->format('Ymd');
231
        } elseif ($object instanceof Voided) {
232
            $parts[] = 'RA';
233
            $parts[] = $object->getFecComunicacion()->format('Ymd');
234
        }
235
        $parts[] = $object->getCorrelativo();
236
237
        return join('-', $parts);
238
    }
239
}