Completed
Push — master ( a291fe...ff3596 )
by Giancarlos
02:48
created

FeSunat::extractResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
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 implements WsSunatInterface
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
    /**
30
     * FeSunat constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->setUrlWsdl(FeSunat::WSDL_ENDPOINT);
35
    }
36
37
    /**
38
     * @param $filename
39
     * @param $content
40
     * @return BillResult
41
     */
42
    public function send($filename, $content)
43
    {
44
        $client = $this->getClient();
45
        $result = new BillResult();
46
47
        try {
48
            $params = [
49
                'fileName' => $filename,
50
                'contentFile' => $content,
51
            ];
52
            $response = $client->__soapCall('sendBill', [ 'parameters' => $params ]);
53
54
            $cdrZip = $response->applicationResponse;
55
            $result
56
                ->setCdrResponse($this->extractResponse($cdrZip))
57
                ->setCdrZip($cdrZip)
58
                ->setSuccess(true);
59
        }
60
        catch (\SoapFault $e) {
61
            $result->setError($this->getErrorFromFault($e));
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * @param string $filename
69
     * @param string $content
70
     * @return SummaryResult
71
     */
72
    public function sendSummary($filename, $content)
73
    {
74
        $client = $this->getClient();
75
        $result = new SummaryResult();
76
77
        try {
78
            $params = [
79
                'fileName' => $filename,
80
                'contentFile' => $content,
81
            ];
82
            $response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]);
83
            $result
84
                ->setTicket($response->ticket)
85
                ->setSuccess(true);
86
        }
87
        catch (\SoapFault $e) {
88
            $result->setError($this->getErrorFromFault($e));
89
        }
90
        return $result;
91
    }
92
93
    /**
94
     * @param string $ticket
95
     * @return StatusResult
96
     */
97
    public function getStatus($ticket)
98
    {
99
        $client = $this->getClient();
100
        $result = new StatusResult();
101
102
        try {
103
            $params = [
104
                'ticket' => $ticket,
105
            ];
106
            $response = $client->__soapCall('getStatus', [ 'parameters' => $params ]);
107
            $status = $response->statusResponse;
108
            $cdrZip = $status->content;
109
110
            $result
111
                ->setCode($status->statusCode)
112
                ->setCdrResponse($this->extractResponse($cdrZip))
113
                ->setCdrZip($cdrZip)
114
                ->setSuccess(true);
115
        }
116
        catch (\SoapFault $e) {
117
            $result->setError($this->getErrorFromFault($e));
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * Get error from Fault Exception.
125
     *
126
     * @param \SoapFault $fault
127
     * @return Error
128
     */
129
    private function getErrorFromFault(\SoapFault $fault)
130
    {
131
        $err = new Error();
132
        $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...
133
        $code = preg_replace('/[^0-9]+/', '', $fcode);
134
135
        if (!$code) {
136
            $err->setCode($fcode);
137
            $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...
138
            return $err;
139
        }
140
141
        $search = new XmlErrorReader();
142
        $msg = $search->getMessageByCode(intval($code));
143
        $err->setCode($code);
144
        $err->setMessage($msg);
145
        return $err;
146
    }
147
148
    private function extractResponse($zipContent)
149
    {
150
        $zip = new ZipFactory();
151
        $xml = $zip->decompressLastFile($zipContent);
152
        $reader = new DomCdrReader();
153
154
        return $reader->getCdrResponse($xml);
155
    }
156
}