Completed
Push — master ( f4e7d7...63b99e )
by Giancarlos
06:02
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
    /**
25
     * @param $filename
26
     * @param $content
27
     * @return BillResult
28
     */
29 8
    public function send($filename, $content)
30
    {
31 8
        $client = $this->getClient();
32 8
        $result = new BillResult();
33
34
        try {
35 6
            $params = [
36 8
                'fileName' => $filename,
37 8
                'contentFile' => $content,
38 8
            ];
39 8
            $response = $client->__soapCall('sendBill', [ 'parameters' => $params ]);
40
41 4
            $cdrZip = $response->applicationResponse;
42
            $result
43 4
                ->setCdrResponse($this->extractResponse($cdrZip))
44 4
                ->setCdrZip($cdrZip)
45 4
                ->setSuccess(true);
46
        }
47 8
        catch (\SoapFault $e) {
48 4
            $result->setError($this->getErrorFromFault($e));
49
        }
50
51 8
        return $result;
52
    }
53
54
    /**
55
     * @param string $filename
56
     * @param string $content
57
     * @return SummaryResult
58
     */
59 4
    public function sendSummary($filename, $content)
60
    {
61 2
        $client = $this->getClient();
62 2
        $result = new SummaryResult();
63
64
        try {
65
            $params = [
66 4
                'fileName' => $filename,
67 2
                'contentFile' => $content,
68 2
            ];
69 2
            $response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]);
70
            $result
71
                ->setTicket($response->ticket)
72
                ->setSuccess(true);
73
        }
74 2
        catch (\SoapFault $e) {
75 2
            $result->setError($this->getErrorFromFault($e));
76
        }
77 2
        return $result;
78
    }
79
80
    /**
81
     * @param string $ticket
82
     * @return StatusResult
83
     */
84
    public function getStatus($ticket)
85
    {
86
        $client = $this->getClient();
87
        $result = new StatusResult();
88
89
        try {
90
            $params = [
91
                'ticket' => $ticket,
92
            ];
93
            $response = $client->__soapCall('getStatus', [ 'parameters' => $params ]);
94
            $status = $response->status;
95
            $cdrZip = $status->content;
96
97
            $result
98
                ->setCode($status->statusCode)
99
                ->setCdrResponse($this->extractResponse($cdrZip))
100
                ->setCdrZip($cdrZip)
101
                ->setSuccess(true);
102
        }
103
        catch (\SoapFault $e) {
104
            $result->setError($this->getErrorFromFault($e));
105
        }
106
107
        return $result;
108
    }
109
110
    /**
111
     * Set Credentials for WebService Authentication.
112
     *
113
     * @param string $user
114
     * @param string $password
115
     */
116 16
    public function setCredentials($user, $password)
117
    {
118 16
       parent::setCredentials($user, $password);
119 16
    }
120
121
    /**
122
     * Get error from Fault Exception.
123
     *
124
     * @param \SoapFault $fault
125
     * @return Error
126
     */
127 6
    private function getErrorFromFault(\SoapFault $fault)
128
    {
129 6
        $err = new Error();
130 6
        $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 6
        $code = preg_replace('/[^0-9]+/', '', $fcode);
132 6
        $msg = '';
133
134 6
        if ($code) {
135
            $msg = $this->getMessageError($code);
136
            $fcode = $code;
137
        } else {
138 6
            $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...
139
140 6
            if ($code) {
141 6
                $msg = $this->getMessageError($code);
142 6
                $fcode = $code;
143 6
            }
144
        }
145
146 6
        if (!$msg) {
147
            $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...
148
        }
149
150 6
        $err->setCode($fcode);
151 6
        $err->setMessage($msg);
152
153 6
        return $err;
154
    }
155
156 6
    private function getMessageError($code)
157
    {
158 6
        $search = new XmlErrorReader();
159 6
        $msg = $search->getMessageByCode(intval($code));
160
161 6
        return $msg;
162
    }
163
164 4
    private function extractResponse($zipContent)
165
    {
166 4
        $zip = new ZipFactory();
167 4
        $xml = $zip->decompressLastFile($zipContent);
168 4
        $reader = new DomCdrReader();
169
170 4
        return $reader->getCdrResponse($xml);
171
    }
172
}