Completed
Pull Request — master (#3)
by Jorge
04:23 queued 03:05
created

FinkokDriver::mapToAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace FeiMx\Pac\Drivers;
4
5
use FeiMx\Pac\Contracts\PacDriverInterface;
6
use FeiMx\Pac\Exceptions\PacErrorException;
7
use FeiMx\Pac\Exceptions\PacVerificationFailedException;
8
use FeiMx\Pac\PacStamp;
9
use FeiMx\Pac\PacUser;
10
use Illuminate\Support\Carbon;
11
use Illuminate\Support\Facades\Validator;
12
13
class FinkokDriver extends AbstractDriver implements PacDriverInterface
14
{
15
    public function stamp($xml): PacStamp
16
    {
17
        $response = $this->request(
18
            $this->url('stamp'),
19
            'Stamp',
20
            $this->prepareStampParams(['xml' => $xml])
21
        );
22
23
        if (is_a($response, 'SoapFault')) {
24
            throw new PacErrorException($response->faultstring);
25
        }
26
27
        if (!isset($response->stampResult->UUID)) {
28
            throw new PacErrorException(
29
                $response->stampResult->Incidencias->Incidencia->MensajeIncidencia,
30
                $response->stampResult->Incidencias->Incidencia->CodigoError
31
            );
32
        }
33
34
        return (new PacStamp())->map($this->stampResultToAttributes($response->stampResult));
35
    }
36
37
    public function cancel(array $uuids, $rfc, $cer, $key)
38
    {
39
        $response = $this->request(
40
            $this->url('cancel'),
41
            'cancel',
42
            $this->prepareStampParams([
43
                'UUIDS' => ['uuids' => $uuids],
44
                'taxpayer_id' => $rfc,
45
                'cer' => $cer,
46
                'key' => $key,
47
            ])
48
        );
49
50
        if (is_a($response, 'SoapFault')) {
51
            throw new PacErrorException($response->faultstring);
52
        }
53
54
        if (isset($response->cancelResult->CodEstatus)) {
55
            throw new PacErrorException($response->cancelResult->CodEstatus);
56
        }
57
58
        return $this->cancelResultToAttributes($response->cancelResult);
59
    }
60
61
    public function addUser($rfc, $params = []): PacUser
62
    {
63
        $this->throwErrorIfInvalidParams($data = array_merge(['rfc' => $rfc], $params));
64
65
        $response = $this->request(
66
            $this->url('registration'),
67
            'add',
68
            $this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params))
69
        );
70
71
        if (is_a($response, 'SoapFault')) {
72
            throw new PacErrorException($response->faultstring);
73
        }
74
75
        if (!$response->addResult->success) {
76
            throw new PacErrorException($response->addResult->message);
77
        }
78
79
        if ('Account Already exists' == $response->addResult->message) {
80
            throw new PacErrorException('The RFC has been registered before');
81
        }
82
83
        return (new PacUser())->map($data);
84
    }
85
86 View Code Duplication
    public function editUser($rfc, $params = [])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $response = $this->request(
89
            $this->url('registration'),
90
            'edit',
91
            $this->prepareGenericParams(array_merge(['taxpayer_id' => $rfc], $params))
92
        );
93
94
        if (is_a($response, 'SoapFault')) {
95
            throw new PacErrorException($response->faultstring);
96
        }
97
98
        if (!$response->editResult->success) {
99
            throw new PacErrorException($response->editResult->message);
100
        }
101
102
        return $response->editResult->message;
103
    }
104
105
    public function getUsers(): array
106
    {
107
        $response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => '']));
108
109
        if (is_a($response, 'SoapFault')) {
110
            throw new PacErrorException($response->faultstring);
111
        }
112
113
        $users = [];
114
        foreach ($response->getResult->users->ResellerUser as $resellerUser) {
115
            $users[] = (new PacUser())->map(
116
                $this->mapToAttributes($resellerUser)
117
            );
118
        }
119
120
        return $users;
121
    }
122
123
    public function getUser($rfc = null): PacUser
124
    {
125
        $response = $this->request($this->url('registration'), 'get', $this->prepareGenericParams(['taxpayer_id' => $rfc]));
126
127
        if (is_a($response, 'SoapFault')) {
128
            throw new PacErrorException($response->faultstring);
129
        }
130
131
        return (new PacUser())->map(
132
            $this->mapToAttributes($response->getResult->users->ResellerUser)
133
        );
134
    }
135
136 View Code Duplication
    public function assignStamps($rfc = null, $credit = 0)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        $response = $this->request(
139
            $this->url('registration'),
140
            'assign',
141
            $this->prepareStampParams(['taxpayer_id' => $rfc, 'credit' => $credit])
142
        );
143
144
        if (is_a($response, 'SoapFault')) {
145
            throw new PacErrorException($response->faultstring);
146
        }
147
148
        if (!$response->assignResult->success) {
149
            throw new PacErrorException($response->assignResult->message);
150
        }
151
152
        return $response->assignResult->message;
153
    }
154
155
    protected function url($wsdl = null)
156
    {
157
        return $this->sandbox
158
            ? "https://demo-facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl"
159
            : "https://facturacion.finkok.com/servicios/soap/{$wsdl}.wsdl";
160
    }
161
162
    protected function throwErrorIfInvalidParams($params = [])
163
    {
164
        $rules = [
165
            'rfc' => 'required',
166
            'type_user' => 'required|in:O,P',
167
            'added' => 'required',
168
        ];
169
170
        if (Validator::make($params, $rules)->fails()) {
171
            throw new PacVerificationFailedException('The params did not contain the necessary fields');
172
        }
173
    }
174
175
    protected function prepareGenericParams(array $params = []): array
176
    {
177
        return array_merge([
178
            'reseller_username' => $this->username,
179
            'reseller_password' => $this->password,
180
        ], $params);
181
    }
182
183
    protected function prepareStampParams(array $params = []): array
184
    {
185
        return array_merge([
186
            'username' => $this->username,
187
            'password' => $this->password,
188
        ], $params);
189
    }
190
191
    protected function stampResultToAttributes($stampResult): array
192
    {
193
        return [
194
            'xml' => $stampResult->xml,
195
            'uuid' => $stampResult->UUID,
196
            'date' => Carbon::parse($stampResult->Fecha),
197
            'statusCode' => $stampResult->CodEstatus,
198
            'satSeal' => $stampResult->SatSeal,
199
            'satCertificateNumber' => $stampResult->NoCertificadoSAT,
200
        ];
201
    }
202
203
    protected function cancelResultToAttributes($cancelResult): array
204
    {
205
        return [
206
            'folios' => $cancelResult->Folios,
207
            'acuse' => $cancelResult->Acuse,
208
            'date' => Carbon::parse($cancelResult->Fecha),
209
            'rfc' => $cancelResult->RfcEmisor,
210
        ];
211
    }
212
213
    protected function mapToAttributes($resellerUser): array
214
    {
215
        return [
216
            'status' => $resellerUser->status,
217
            'counter' => $resellerUser->counter,
218
            'credit' => $resellerUser->credit,
219
            'rfc' => $resellerUser->taxpayer_id,
220
        ];
221
    }
222
}
223