Test Failed
Push — master ( 05d675...f4e7d7 )
by Giancarlos
04:02
created

FeSunat::send()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0758

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 11
cts 15
cp 0.7332
rs 8.9713
c 1
b 0
f 0
cc 2
eloc 16
nc 3
nop 2
crap 2.0758
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 2
    public function send($filename, $content)
30
    {
31 2
        $client = $this->getClient();
32 2
        $result = new BillResult();
33
34
        try {
35 2
            $params = [
36 2
                'fileName' => $filename,
37 2
                'contentFile' => $content,
38 2
            ];
39 2
            $response = $client->__soapCall('sendBill', [ 'parameters' => $params ]);
40
41
            $cdrZip = $response->applicationResponse;
42
            $result
43
                ->setCdrResponse($this->extractResponse($cdrZip))
44
                ->setCdrZip($cdrZip)
45
                ->setSuccess(true);
46
        }
47 2
        catch (\SoapFault $e) {
48 2
            $result->setError($this->getErrorFromFault($e));
49
        }
50
51 2
        return $result;
52
    }
53
54
    /**
55
     * @param string $filename
56
     * @param string $content
57
     * @return SummaryResult
58
     */
59 2
    public function sendSummary($filename, $content)
60
    {
61
        $client = $this->getClient();
62
        $result = new SummaryResult();
63
64
        try {
65
            $params = [
66 2
                'fileName' => $filename,
67
                'contentFile' => $content,
68
            ];
69
            $response = $client->__soapCall('sendSummary', [ 'parameters' => $params ]);
70
            $result
71
                ->setTicket($response->ticket)
72
                ->setSuccess(true);
73
        }
74
        catch (\SoapFault $e) {
75
            $result->setError($this->getErrorFromFault($e));
76
        }
77
        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 2
    public function setCredentials($user, $password)
117
    {
118 2
       parent::setCredentials($user, $password);
119 2
    }
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 2
        $msg = '';
133
134 2
        if ($code) {
135
            $msg = $this->getMessageError($code);
136
            $fcode = $code;
137
        } else {
138 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...
139
140 2
            if ($code) {
141 2
                $msg = $this->getMessageError($code);
142 2
                $fcode = $code;
143 2
            }
144
        }
145
146 2
        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 2
        $err->setCode($fcode);
151 2
        $err->setMessage($msg);
152
153 2
        return $err;
154
    }
155
156 2
    private function getMessageError($code)
157
    {
158 2
        $search = new XmlErrorReader();
159 2
        $msg = $search->getMessageByCode(intval($code));
160
161 2
        return $msg;
162
    }
163
164
    private function extractResponse($zipContent)
165
    {
166
        $zip = new ZipFactory();
167
        $xml = $zip->decompressLastFile($zipContent);
168
        $reader = new DomCdrReader();
169
170
        return $reader->getCdrResponse($xml);
171
    }
172
}