Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.72% covered (warning)
78.72%
37 / 47
72.73% covered (warning)
72.73%
8 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
SeniorImportBase
78.72% covered (warning)
78.72%
37 / 47
72.73% covered (warning)
72.73%
8 / 11
14.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 convertBase64InArray
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 verifyErroFromSenior
14.29% covered (danger)
14.29%
1 / 7
0.00% covered (danger)
0.00%
0 / 1
8.67
 logSoapInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 logSoapError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 elapsedMilliseconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeSoapContext
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 resolveSoapReturnCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resolveSoapRequestNumber
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resolveSoapErrorMessage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 responseLogContext
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\Shared\Integrations\Senior;
4
5use Domain\Shared\Integrations\Senior\Exceptions\IntegrationSoapSeniorException;
6use Illuminate\Support\Facades\Log;
7
8class SeniorImportBase
9{
10    public array $options;
11
12    public function __construct()
13    {
14        $this->options = [
15            'exceptions' => true,
16            'trace' => 1,
17            'cache_wsdl' => WSDL_CACHE_NONE,
18            'connection_timeout' => config('integration.soap_connection_timeout', 10),
19            'stream_context' => stream_context_create([
20                'http' => [
21                    'timeout' => config('integration.soap_timeout', 45),
22                ],
23            ]),
24        ];
25    }
26
27    public function convertBase64InArray($base64): array
28    {
29        $base64_decode = base64_decode($base64, true);
30        $data_utf8 = mb_convert_encoding($base64_decode, 'UTF-8', 'WINDOWS-1252');
31
32        return json_decode($data_utf8, true) ?? [];
33    }
34
35    public function verifyErroFromSenior($response, $wsdlURI)
36    {
37        if (isset($response->erroExecucao) || $response->statusProcesso != 1) {
38            throw new IntegrationSoapSeniorException(
39                __("Senior's SOAP request returned with an error: :message (:wsdl)", [
40                    'message' => $response->erroExecucao ?? $response->mensagemProcesso,
41                    'wsdl' => $wsdlURI,
42                ]),
43            );
44        }
45    }
46
47    protected function logSoapInfo(string $event, array $context): void
48    {
49        Log::channel('integration')->info($event, $context);
50    }
51
52    protected function logSoapError(string $event, array $context): void
53    {
54        Log::channel('integration')->error($event, $context);
55    }
56
57    protected function elapsedMilliseconds(float $startedAt): int
58    {
59        return (int) round((microtime(true) - $startedAt) * 1000);
60    }
61
62    protected function makeSoapContext(string $operation, string $wsdlURI, array $payload): array
63    {
64        return array_filter([
65            'operation' => $operation,
66            'wsdl' => $wsdlURI,
67            'identificador_externo' => $payload['dadosGerais']['identificadorExterno']
68                ?? $payload['identificadorExterno']
69                ?? null,
70            'num_eme' => $payload['dadosGerais']['numEme']
71                ?? $payload['requisicao']['numEme']
72                ?? null,
73            'tipo_operacao' => $payload['dadosGerais']['tipoOperacao'] ?? null,
74            'tipo_processamento' => $payload['dadosGerais']['tipoProcessamento']
75                ?? $payload['tipoProcessamento']
76                ?? null,
77        ], static fn ($value) => $value !== null);
78    }
79
80    protected function resolveSoapReturnCode(?object $response): ?int
81    {
82        return $response?->tipoRetorno ?? $response?->retorno?->tipoRetorno ?? null;
83    }
84
85    protected function resolveSoapRequestNumber(?object $response): ?int
86    {
87        return $response?->numEme ?? $response?->retorno?->numEme ?? null;
88    }
89
90    protected function resolveSoapErrorMessage(?object $response): ?string
91    {
92        return $response?->erroExecucao
93            ?? $response?->erros?->msgErr
94            ?? $response?->mensagemRetorno
95            ?? $response?->retorno?->itensRetorno?->mensagemRetorno
96            ?? null;
97    }
98
99    protected function responseLogContext(?object $response): array
100    {
101        return array_filter([
102            'response_num_eme' => $this->resolveSoapRequestNumber($response),
103            'response_tipo_retorno' => $this->resolveSoapReturnCode($response),
104            'response_message' => $this->resolveSoapErrorMessage($response),
105        ], static fn ($value) => $value !== null);
106    }
107}