Completed
Push — master ( 0a5559...4dd4d6 )
by Giancarlos
04:06
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 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 54
    public function __construct()
33
    {
34 54
        $this->setUrlWsdl(FeSunat::WSDL_ENDPOINT);
35 54
    }
36
37
    /**
38
     * @param $filename
39
     * @param $content
40
     * @return BillResult
41
     */
42 22
    public function send($filename, $content)
43
    {
44 22
        $client = $this->getClient();
45 22
        $result = new BillResult();
46
47
        try {
48
            $params = [
49 22
                'fileName' => $filename,
50 22
                'contentFile' => $content,
51 22
            ];
52 22
            $response = $client->__soapCall('sendBill', [ 'parameters' => $params ]);
53
54 18
            $cdrZip = $response->applicationResponse;
55
            $result
56 18
                ->setCdrResponse($this->extractResponse($cdrZip))
57 18
                ->setCdrZip($cdrZip)
58 18
                ->setSuccess(true);
59
        }
60 22
        catch (\SoapFault $e) {
61 4
            $result->setError($this->getErrorFromFault($e));
62
        }
63
64 22
        return $result;
65
    }
66
67
    /**
68
     * @param string $filename
69
     * @param string $content
70
     * @return SummaryResult
71
     */
72 10
    public function sendSummary($filename, $content)
73
    {
74 10
        $client = $this->getClient();
75 10
        $result = new SummaryResult();
76
77
        try {
78
            $params = [
79 10
                'fileName' => $filename,
80 10
                'contentFile' => $content,
81 10
            ];
82 10
            $response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]);
83
            $result
84 6
                ->setTicket($response->ticket)
85 6
                ->setSuccess(true);
86
        }
87 10
        catch (\SoapFault $e) {
88 4
            $result->setError($this->getErrorFromFault($e));
89
        }
90 10
        return $result;
91
    }
92
93
    /**
94
     * @param string $ticket
95
     * @return StatusResult
96
     */
97 4
    public function getStatus($ticket)
98
    {
99 4
        $client = $this->getClient();
100 4
        $result = new StatusResult();
101
102
        try {
103
            $params = [
104 4
                'ticket' => $ticket,
105 4
            ];
106 4
            $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 4
        catch (\SoapFault $e) {
117 4
            $result->setError($this->getErrorFromFault($e));
118
        }
119
120 4
        return $result;
121
    }
122
123
    /**
124
     * Set Credentials for WebService Authentication.
125
     *
126
     * @param string $user
127
     * @param string $password
128
     */
129 54
    public function setCredentials($user, $password)
130
    {
131 54
       parent::setCredentials($user, $password);
132 54
    }
133
134
    /**
135
     * Get error from Fault Exception.
136
     *
137
     * @param \SoapFault $fault
138
     * @return Error
139
     */
140 12
    private function getErrorFromFault(\SoapFault $fault)
141
    {
142 12
        $err = new Error();
143 12
        $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...
144 12
        $code = preg_replace('/[^0-9]+/', '', $fcode);
145 12
        $msg = '';
146
147 12
        if ($code) {
148 8
            $msg = $this->getMessageError($code);
149 8
        } else {
150 4
            $code = preg_replace('/[^0-9]+/', '', $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...
151
152 4
            if ($code) {
153 4
                $msg = $this->getMessageError($code);
154 4
            }
155
        }
156
157 12
        if (!$msg) {
158
            $msg = isset($fault->detail) ? $fault->detail->message : $fault->faultstring;
0 ignored issues
show
Bug introduced by
The property detail 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...
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...
159
        }
160
161 12
        if ($code) {
162 12
            $fcode = $code;
163 12
        }
164
165 12
        $err->setCode($fcode);
166 12
        $err->setMessage($msg);
167
168 12
        return $err;
169
    }
170
171 12
    private function getMessageError($code)
172
    {
173 12
        $search = new XmlErrorReader();
174 12
        $msg = $search->getMessageByCode(intval($code));
175
176 12
        return $msg;
177
    }
178
179 18
    private function extractResponse($zipContent)
180
    {
181 18
        $zip = new ZipFactory();
182 18
        $xml = $zip->decompressLastFile($zipContent);
183 18
        $reader = new DomCdrReader();
184
185 18
        return $reader->getCdrResponse($xml);
186
    }
187
}