Passed
Push — master ( 8cc5c3...c2f62f )
by Giancarlos
03:39
created

FeSunat::setCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 2
            $status = $response->status;
108 2
            $cdrZip = $status->content;
109
110
            $result
111 2
                ->setCode($status->statusCode)
112 2
                ->setCdrResponse($this->extractResponse($cdrZip))
113 2
                ->setCdrZip($cdrZip)
114 2
                ->setSuccess(true);
115
        }
116 4
        catch (\SoapFault $e) {
117 2
            $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 10
    private function getErrorFromFault(\SoapFault $fault)
141
    {
142 10
        $err = new Error();
143 10
        $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 10
        $code = preg_replace('/[^0-9]+/', '', $fcode);
145 10
        $msg = '';
146
147 10
        if ($code) {
148 8
            $msg = $this->getMessageError($code);
149 8
        } else {
150 2
            $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 2
            if ($code) {
153 2
                $msg = $this->getMessageError($code);
154 2
            }
155
        }
156
157 10
        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 10
        if ($code) {
162 10
            $fcode = $code;
163 10
        }
164
165 10
        $err->setCode($fcode);
166 10
        $err->setMessage($msg);
167
168 10
        return $err;
169
    }
170
171 10
    private function getMessageError($code)
172
    {
173 10
        $search = new XmlErrorReader();
174 10
        $msg = $search->getMessageByCode(intval($code));
175
176 10
        return $msg;
177
    }
178
179 20
    private function extractResponse($zipContent)
180
    {
181 20
        $zip = new ZipFactory();
182 20
        $xml = $zip->decompressLastFile($zipContent);
183 20
        $reader = new DomCdrReader();
184
185 20
        return $reader->getCdrResponse($xml);
186
    }
187
}