Completed
Push — master ( ec66da...c95071 )
by Roberto
30:31 queued 15:29
created

Atende::getContribuicaoAnalitica()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 6
1
<?php
2
3
namespace NFePHP\Atende;
4
5
use Exception;
6
use RuntimeException;
7
8
class Atende
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $user;
14
    /**
15
     * @var string
16
     */
17
    protected $password;
18
    /**
19
     * @var int
20
     */
21
    protected $tpAmb = 2;
22
    /**
23
     * @var string
24
     */
25
    protected $soaperror;
26
    /**
27
     * @var string
28
     */
29
    protected $soapinfo;
30
    /**
31
     * @var string
32
     */
33
    public $url = [
34
        1 => "https://{city}.atende.net/atende.php?pg=services"
35
            . "&service=WFPAtuarialPadraoIPM&cidade=padrao",
36
        2 => "https://qualidadeatendenet56.ipm.com.br/rh04/atende.php"
37
            . "?pg=services&service=WFPAtuarialPadraoIPM&cidade={city}"
38
    ];
39
    /**
40
     * @var string
41
     */
42
    public $cidade = 'paranagua';
43
    /**
44
     * @var int
45
     */
46
    public $codcidade = 3037;
47
    /**
48
     * @var int
49
     */
50
    public $soaptimeout = 10;
51
    /**
52
     * @var bool
53
     */
54
    public $debugmode = false;
55
    
56
    /**
57
     * Constructor
58
     * @param string $user
59
     * @param string $password
60
     * @param string $cidade
61
     * @param int $codcidade
62
     */
63
    public function __construct($user, $password, $cidade, $codcidade)
64
    {
65
        $this->user = trim($user);
66
        $this->password = trim($password);
67
        $this->cidade = strtolower(trim($cidade));
68
        $this->codcidade = $codcidade;
69
        if (empty($this->user) || empty($this->password)) {
70
            throw new RuntimeException("A identificação do usuário e a "
71
                . "senha são obrigatórios.");
72
        }
73
    }
74
    
75
    /**
76
     * Set environment 2 to tests and 1 to production
77
     * @param int $tpAmb
78
     * @return int
79
     */
80
    public function setAmbiente($tpAmb = null)
81
    {
82
        if (!empty($tpAmb) && ($tpAmb == 1 || $tpAmb == 2)) {
83
            $this->tpAmb = $tpAmb;
84
        }
85
        return $this->tpAmb;
86
    }
87
    
88
    /**
89
     * Set debugmod
90
     * When debugmod = true, files with soap content will be saved for analises
91
     * @param bool $flag
92
     * @return void
93
     */
94
    public function setDebugMode($flag = true)
95
    {
96
        if (!empty($flag)) {
97
            $this->debugmode = $flag;
98
        }
99
        return $this->debugmode;
100
    }
101
    
102
    /**
103
     * Set soap timeout parameter in seconds
104
     * @param int $sec
105
     * @return void
106
     */
107
    public function setSoapTimeOut($sec = 10)
108
    {
109
        if (!empty($sec)) {
110
            $this->soaptimeout = $sec;
111
        }
112
        return $this->soaptimeout;
113
    }
114
    
115
    /**
116
     * Consulta Dados dos Afastamentos do Funcionário (getAfastamento)
117
     * Retorna os dados dos afastamentos de um ou mais funcionários
118
     * cadastrados no módulo da Folha de Pagamento do sistema Atende.Net.
119
     *
120
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
121
     * @param int $pagina
122
     * @param string $cpf
123
     * @param string $numeroMatricula
124
     * @param string $tipo
125
     * @param int $codigoRegime
126
     */
127 View Code Duplication
    public function getAfastamento(
0 ignored issues
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...
128
        $anomes,
129
        $pagina = null,
130
        $cpf = null,
131
        $numeroMatricula = null,
132
        $tipo = null,
133
        $codigoRegime = null
134
    ) {
135
        $action = "getAfastamento";
136
        $message = $this->build(
137
            $action,
138
            $anomes,
139
            $pagina,
140
            $cpf,
141
            $numeroMatricula,
142
            $tipo,
143
            $codigoRegime
144
        );
145
        $response = $this->send($action, $message);
146
        return $response;
147
    }
148
    
149
    /**
150
     * Consulta Dados das Bases de Contribuição (getContribuicao)
151
     * Retorna os dados das bases de pagamento de um ou mais funcionários
152
     * cadastrados no módulo da Folha de Pagamento do sistema Atende.Net
153
     *
154
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
155
     * @param int $pagina
156
     * @param string $cpf
157
     * @param string $numeroMatricula
158
     * @param string $tipo
159
     * @param int $codigoRegime
160
     */
161 View Code Duplication
    public function getContribuicao(
0 ignored issues
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...
162
        $anomes,
163
        $pagina = null,
164
        $cpf = null,
165
        $numeroMatricula = null,
166
        $tipo = null,
167
        $codigoRegime = null
168
    ) {
169
        $action = "getContribuicao";
170
        $message = $this->build(
171
            $action,
172
            $anomes,
173
            $pagina,
174
            $cpf,
175
            $numeroMatricula,
176
            $tipo,
177
            $codigoRegime
178
        );
179
        $response = $this->send($action, $message);
180
        return $response;
181
    }
182
    
183
    /**
184
     * Consulta Dados Pagamentos (getContribuicaoAnalitica)
185
     * Retorna os dados das folhas de pagamento de um ou mais funcionários
186
     * cadastrados no módulo da Folha de Pagamento do sistema Atende.Net
187
     * especificando verbas, referências e valores.
188
     *
189
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
190
     * @param int $pagina
191
     * @param string $cpf
192
     * @param string $numeroMatricula
193
     * @param string $tipo
194
     * @param int $codigoRegime
195
     */
196 View Code Duplication
    public function getContribuicaoAnalitica(
0 ignored issues
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...
197
        $anomes,
198
        $pagina = null,
199
        $cpf = null,
200
        $numeroMatricula = null,
201
        $tipo = null,
202
        $codigoRegime = null
203
    ) {
204
        $action = "getContribuicaoAnalitica";
205
        $message = $this->build(
206
            $action,
207
            $anomes,
208
            $pagina,
209
            $cpf,
210
            $numeroMatricula,
211
            $tipo,
212
            $codigoRegime
213
        );
214
        $response = $this->send($action, $message);
215
        return $response;
216
    }
217
    
218
    /**
219
     * Dados dos Dependentes do Funcionário (getDependentes)
220
     * Retorna os dados dos dependentes dos funcionários e do relacionamento
221
     * de dependência no sistema Atende.Net.
222
     *
223
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
224
     * @param int $pagina
225
     * @param string $cpf
226
     * @param string $numeroMatricula
227
     * @param string $tipo
228
     * @param int $codigoRegime
229
     */
230 View Code Duplication
    public function getDependentes(
0 ignored issues
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...
231
        $anomes,
232
        $pagina = null,
233
        $cpf = null,
234
        $numeroMatricula = null,
235
        $tipo = null,
236
        $codigoRegime = null
237
    ) {
238
        $action = "getDependentes";
239
        $message = $this->build(
240
            $action,
241
            $anomes,
242
            $pagina,
243
            $cpf,
244
            $numeroMatricula,
245
            $tipo,
246
            $codigoRegime
247
        );
248
        $response = $this->send($action, $message);
249
        return $response;
250
    }
251
    
252
    /**
253
     * Consulta Dados de Funcionário (getServidor)
254
     * Retorna os dados cadastrais de um ou mais funcionários do módulo
255
     * da Folha de Pagamento do sistema Atende.Net. Se o funcionário for
256
     * aposentado ou pensionista trará informações do tipo de aposentadoria ou
257
     * tipo de pensão. Caso o funcionário pensionista seja responsável ou
258
     * dependente de um funcionário falecido no relacionamento
259
     * Funcionário x Pensão do tipo "Por Morte" trará também os dados do
260
     * funcionário falecido.
261
     *
262
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
263
     * @param int $pagina
264
     * @param string $cpf
265
     * @param string $numeroMatricula
266
     * @param string $tipo
267
     * @param int $codigoRegime
268
     */
269 View Code Duplication
    public function getServidor(
0 ignored issues
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...
270
        $anomes,
271
        $pagina = null,
272
        $cpf = null,
273
        $numeroMatricula = null,
274
        $tipo = null,
275
        $codigoRegime = null
276
    ) {
277
        $action = "getServidor";
278
        $message = $this->build(
279
            $action,
280
            $anomes,
281
            $pagina,
282
            $cpf,
283
            $numeroMatricula,
284
            $tipo,
285
            $codigoRegime
286
        );
287
        $response = $this->send($action, $message);
288
        return $response;
289
    }
290
    
291
    /**
292
     * Consulta Dados Tempo de Contribuição do Funcionário
293
     * (getTempoContribuicao)
294
     * Retorna os dados dos tempos de contribuição anteriores dos funcionários
295
     * cadastrados no sistema Atende.Net
296
     *
297
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
298
     * @param int $pagina
299
     * @param string $cpf
300
     * @param string $numeroMatricula
301
     * @param string $tipo
302
     * @param int $codigoRegime
303
     */
304 View Code Duplication
    public function getTempoContribuicao(
0 ignored issues
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...
305
        $anomes,
306
        $pagina = null,
307
        $cpf = null,
308
        $numeroMatricula = null,
309
        $tipo = null,
310
        $codigoRegime = null
311
    ) {
312
        $action = "getTempoContribuicao";
313
        $message = $this->build(
314
            $action,
315
            $anomes,
316
            $pagina,
317
            $cpf,
318
            $numeroMatricula,
319
            $tipo,
320
            $codigoRegime
321
        );
322
        $response = $this->send($action, $message);
323
        return $response;
324
    }
325
    
326
    /**
327
     * Build message content
328
     *
329
     * @param string $action (OBRIGATÓRIO)
330
     * @param int $anomes (OBRIGATÓRIO) ex. 201612
331
     * @param int $pagina
332
     * @param string $cpf
333
     * @param string $numeroMatricula
334
     * @param int $tipo
335
     * @param int $codigoRegime
336
     * @return string
337
     */
338
    protected function build(
339
        $action,
340
        $anomes,
341
        $pagina = null,
342
        $cpf = null,
343
        $numeroMatricula = null,
344
        $tipo = null,
345
        $codigoRegime = null
346
    ) {
347
        $message = "<net:$action "
348
        . "soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
349
        . "<parametro xsi:type=\"net:ParametrosAvaliacaoAtuarial\">"
350
        . "<codigoCliente xsi:type=\"xsd:int\">$this->codcidade</codigoCliente>"
351
        . "<mesAno xsi:type=\"xsd:int\">$anomes</mesAno>";
352
        if (!empty($pagina)) {
353
            $message .= "<pagina xsi:type=\"xsd:int\">$pagina</pagina>";
354
        }
355
        if (!empty($cpf)) {
356
            $message .= "<cpf xsi:type=\"xsd:string\">$cpf</cpf>";
357
        }
358
        if (!empty($numeroMatricula)) {
359
            $message .= "<numeroMatricula "
360
            . "xsi:type=\"xsd:string\">$numeroMatricula</numeroMatricula>";
361
        }
362
        if (!empty($tipo)) {
363
            $message .= "<tipo xsi:type=\"xsd:int\">$tipo</tipo>";
364
        }
365
        if (!empty($codigoRegime)) {
366
            $message .= "<codigoRegime "
367
            . "xsi:type=\"xsd:int\">$codigoRegime</codigoRegime>";
368
        }
369
        $message .= "</parametro></net:$action>";
370
        return $message;
371
    }
372
373
    /**
374
     * Send SOAP message, and get response from webservice
375
     * @param string $action
376
     * @param string $message
377
     * @return string
378
     * @throws \RuntimeException
379
     */
380
    protected function send($action, $message)
381
    {
382
        $envelope = "<soapenv:Envelope "
383
        . "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
384
        . "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
385
        . "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
386
        . "xmlns:net=\"net.atende\">"
387
        . "<soapenv:Header/>"
388
        . "<soapenv:Body>";
389
        $envelope .= $message . "</soapenv:Body></soapenv:Envelope>";
390
        
391
        $contentLength = strlen($envelope);
392
        $headers = [
393
            'User-Agent: PHP-SOAP',
394
            'Accept-Encoding: gzip,deflate',
395
            'Content-Type: text/xml;charset=UTF-8',
396
            "SOAPAction: \"net.atende#$action\"",
397
            "Content-Length: $contentLength",
398
            'Expect: 100-continue',
399
            'Connection: Keep-Alive'
400
        ];
401
        
402
        $txtheaders = implode("\n", $headers);
403
        $url = $this->url[$this->tpAmb];
404
        $url = str_replace("{city}", $this->cidade, $url);
405
406
        try {
407
            $oCurl = curl_init();
408
            curl_setopt($oCurl, CURLOPT_URL, $url);
409
            curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, $this->soaptimeout);
410
            curl_setopt($oCurl, CURLOPT_TIMEOUT, $this->soaptimeout + 20);
411
            //dont use security check
412
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, 0);
413
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, 0);
414
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
415
            curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
416
            curl_setopt($oCurl, CURLOPT_FAILONERROR, 0);
417
            curl_setopt($oCurl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
418
            curl_setopt($oCurl, CURLOPT_USERPWD, $this->user.":".$this->password);
419
            curl_setopt($oCurl, CURLOPT_POST, 1);
420
            curl_setopt($oCurl, CURLOPT_POSTFIELDS, $envelope);
421
            curl_setopt($oCurl, CURLOPT_HEADER, 1);
422
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
423
            curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
424
            curl_setopt($oCurl, CURLOPT_CERTINFO, 1);
425
            
426
            $response = curl_exec($oCurl);
427
            
428
            $this->soaperror = curl_error($oCurl);
429
            $this->soapinfo = curl_getinfo($oCurl);
430
            
431
            $headsize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
432
            $httpcode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
433
            $bodysize = curl_getinfo($oCurl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
0 ignored issues
show
Unused Code introduced by
$bodysize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
434
            $bodyType = curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
0 ignored issues
show
Unused Code introduced by
$bodyType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
435
            
436
            curl_close($oCurl);
437
            
438
            $responseHead = substr($response, 0, $headsize);
439
            $responseBody = substr($response, $headsize);
440
            if (substr($response, -3) !== substr($responseBody, -3)) {
441
                throw new \RuntimeException(
442
                    'A terminação dos dados compactados está diferente! Não foi possivel extrair os dados.'
443
                );
444
            }
445
            $marker = substr($responseBody, 0, 3);
446
            //identify compress body in gzip deflate
447
            if ($marker === chr(0x1F).chr(0x8B).chr(0x08)) {
448
                $responseBody = gzdecode($responseBody);
449
            }
450
            $this->saveDebugFiles(
451
                $action,
452
                $txtheaders . "\n" . $envelope,
453
                $responseHead . "\n" . $responseBody,
454
                $this->debugmode
455
            );
456
        } catch (\Exception $e) {
457
            throw new \RuntimeException($e->getMessage());
458
        }
459
        if ($this->soaperror != '') {
460
            throw new \RuntimeException($this->soaperror);
461
        }
462
        if ($httpcode != 200) {
463
            throw new \RuntimeException($responseHead . '\n' . $responseBody);
464
        }
465
        return $responseBody;
466
    }
467
    
468
    /**
469
     * Save request envelope and response for debug reasons
470
     * @param string $action
471
     * @param string $request
472
     * @param string $response
473
     * @return void
474
     */
475
    protected function saveDebugFiles($action, $request, $response, $flag = false)
476
    {
477
        if (!$flag) {
478
            return;
479
        }
480
        $tempdir = sys_get_temp_dir()
481
            . DIRECTORY_SEPARATOR
482
            . 'soap'
483
            . DIRECTORY_SEPARATOR;
484
        if (! is_dir($tempdir)) {
485
            mkdir($tempdir, 0777);
486
        }
487
        $num = date('mdHis');
488
        file_put_contents($tempdir . "req_" . $action . "_" . $num . ".txt", $request);
489
        file_put_contents($tempdir . "res_" . $action . "_" . $num . ".txt", $response);
490
    }
491
}
492