Passed
Push — master ( d54f6c...8277b0 )
by Giancarlos
02:59
created

FeSunat::getStatus()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 9
cts 15
cp 0.6
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 3
nop 1
crap 2.2559
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 15/07/2017
6
 * Time: 22:56
7
 */
8
9
namespace Greenter\Ws\Services;
10
use Greenter\Model\Response\StatusResult;
11
use Greenter\Model\Response\SummaryResult;
12
use Greenter\Ws\Reader\DomCdrReader;
13
use Greenter\Ws\Reader\XmlErrorReader;
14
use Greenter\Zip\ZipFactory;
15
use Greenter\Model\Response\BillResult;
16
use Greenter\Model\Response\Error;
17
18
/**
19
 * Class FeSunat
20
 * @package Greenter\Ws\Services
21
 */
22
class FeSunat extends BaseSunat
23
{
24
    const BETA = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService';
25
    const HOMOLOGACION  = 'https://www.sunat.gob.pe/ol-ti-itcpgem-sqa/billService';
26
    const PRODUCCION = 'https://e-factura.sunat.gob.pe/ol-ti-itcpfegem/billService';
27
    const WSDL_ENDPOINT = 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl';
28
29 6
    public function __construct($user, $password)
30
    {
31 6
        parent::__construct($user, $password);
32 6
        $this->setUrlWsdl(FeSunat::WSDL_ENDPOINT);
33 6
    }
34
35
    /**
36
     * @param $filename
37
     * @param $content
38
     * @return BillResult
39
     */
40 2
    public function send($filename, $content)
41
    {
42 2
        $client = $this->getClient();
43 2
        $result = new BillResult();
44
45
        try {
46
            $params = [
47 2
                'fileName' => $filename,
48 2
                'contentFile' => $content,
49 2
            ];
50 2
            $response = $client->__soapCall('sendBill', [ 'parameters' => $params ]);
51
52 2
            $cdrZip = $response->applicationResponse;
53
            $result
54 2
                ->setCdrResponse($this->extractResponse($cdrZip))
55 2
                ->setCdrZip($cdrZip)
56 2
                ->setSuccess(true);
57
        }
58 2
        catch (\SoapFault $e) {
59
            $result->setError($this->getErrorFromFault($e));
60
        }
61
62 2
        return $result;
63
    }
64
65
    /**
66
     * @param string $filename
67
     * @param string $content
68
     * @return SummaryResult
69
     */
70 2
    public function sendSummary($filename, $content)
71
    {
72 2
        $client = $this->getClient();
73 2
        $result = new SummaryResult();
74
75
        try {
76
            $params = [
77 2
                'fileName' => $filename,
78 2
                'contentFile' => $content,
79 2
            ];
80 2
            $response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]);
81
            $result
82 2
                ->setTicket($response->ticket)
83 2
                ->setSuccess(true);
84
        }
85 2
        catch (\SoapFault $e) {
86
            $result->setError($this->getErrorFromFault($e));
87
        }
88 2
        return $result;
89
    }
90
91
    /**
92
     * @param string $ticket
93
     * @return StatusResult
94
     */
95 2
    public function getStatus($ticket)
96
    {
97 2
        $client = $this->getClient();
98 2
        $result = new StatusResult();
99
100
        try {
101
            $params = [
102 2
                'ticket' => $ticket,
103 2
            ];
104 2
            $response = $client->__soapCall('getStatus', [ 'parameters' => $params ]);
105
            $status = $response->statusResponse;
106
            $cdrZip = $status->content;
107
108
            $result
109
                ->setCode($status->statusCode)
110
                ->setCdrResponse($this->extractResponse($cdrZip))
111
                ->setCdrZip($cdrZip)
112
                ->setSuccess(true);
113
        }
114 2
        catch (\SoapFault $e) {
115 2
            $result->setError($this->getErrorFromFault($e));
116
        }
117
118 2
        return $result;
119
    }
120
121
    /**
122
     * Get error from Fault Exception.
123
     *
124
     * @param \SoapFault $fault
125
     * @return Error
126
     */
127 2
    private function getErrorFromFault(\SoapFault $fault)
128
    {
129 2
        $err = new Error();
130 2
        $fcode = $fault->faultcode;
0 ignored issues
show
Bug introduced by
The property faultcode does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
131 2
        $code = preg_replace('/[^0-9]+/', '', $fcode);
132
133 2
        if (!$code) {
134
            $err->setCode($fcode);
135
            $err->setMessage($fault->faultstring);
0 ignored issues
show
Bug introduced by
The property faultstring does not seem to exist. Did you mean string?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
            return $err;
137
        }
138
139 2
        $search = new XmlErrorReader();
140 2
        $msg = $search->getMessageByCode(intval($code));
141 2
        $err->setCode($code);
142 2
        $err->setMessage($msg);
143 2
        return $err;
144
    }
145
146
147 2
    private function extractResponse($zipContent)
148
    {
149 2
        $zip = new ZipFactory();
150 2
        $xml = $zip->decompressLastFile($zipContent);
151 2
        $reader = new DomCdrReader();
152
153 2
        return $reader->getCdrResponse($xml);
154
    }
155
}