Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
59.02% covered (warning)
59.02%
36 / 61
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SeniorRequestDevolutionExport
59.02% covered (warning)
59.02%
36 / 61
0.00% covered (danger)
0.00%
0 / 2
16.88
0.00% covered (danger)
0.00%
0 / 1
 execute
58.49% covered (warning)
58.49%
31 / 53
0.00% covered (danger)
0.00%
0 / 1
12.58
 retrieveUserAndPassword
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
1<?php
2
3namespace Domain\Shared\Integrations\Senior;
4
5use Domain\Request\DTOs\ExportDevolutionRequestSeniorDTO;
6use Domain\Shared\Integrations\Senior\Exceptions\IntegrationSoapSeniorException;
7use Illuminate\Support\Facades\Auth;
8use Illuminate\Support\Facades\Cache;
9use Illuminate\Support\Facades\Crypt;
10use SoapFault;
11use stdClass;
12
13use function __;
14
15class SeniorRequestDevolutionExport extends SeniorImportBase
16{
17    /**
18     * @param ExportDevolutionRequestSeniorDTO $exportRequestSeniorDTO
19     * @return object|null
20     * @throws IntegrationSoapSeniorException
21     */
22    public function execute(ExportDevolutionRequestSeniorDTO $exportRequestSeniorDTO): object|null
23    {
24        $wsdlURI = config('integration.url') . '/sapiens_Synccom_senior_g5_co_mcm_est_requisicoes?wsdl';
25        $credentials = $this->retrieveUserAndPassword();
26        $payload = $exportRequestSeniorDTO->toArray();
27        $context = $this->makeSoapContext('requisicaoAgrupadaComposta', $wsdlURI, $payload);
28        $startedAt = microtime(true);
29
30        try {
31            $this->logSoapInfo('senior.soap.started', $context);
32
33            $client = new SeniorSoapClient($wsdlURI, $this->options);
34            $this->logSoapInfo('senior.soap.client_ready', array_merge($context, [
35                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
36            ]));
37
38            $response = $client->RequisicaoAgrupadaComposta(
39                $credentials['user'],
40                $credentials['password'],
41                0,
42                $payload
43            );
44
45            $this->logSoapInfo('senior.soap.finished', array_merge($context, [
46                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
47            ], $this->responseLogContext($response)));
48
49            $returnCode = $this->resolveSoapReturnCode($response);
50            if ((isset($response->erroExecucao) || isset($response->erros)) || ($returnCode !== null && $returnCode !== 0)) {
51                $this->logSoapError('senior.soap.functional_error', array_merge($context, [
52                    'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
53                ], $this->responseLogContext($response)));
54
55                throw new IntegrationSoapSeniorException(
56                    __("Senior's SOAP request returned with an error: :message (:wsdl)", [
57                        'message' => $this->resolveSoapErrorMessage($response),
58                        'wsdl' => $wsdlURI,
59                    ]),
60                );
61            }
62
63            $requestNumber = $this->resolveSoapRequestNumber($response);
64            if ($requestNumber && $requestNumber > 0) {
65                $normalizedResponse = (object) array_merge(
66                    (array) ($response->retorno ?? new stdClass()),
67                    [
68                        'numEme' => $requestNumber,
69                        'tipoRetorno' => $returnCode,
70                    ]
71                );
72
73                return $normalizedResponse;
74            }
75
76            return null;
77        } catch (SoapFault $soapFault) {
78            $this->logSoapError('senior.soap.soap_fault', array_merge($context, [
79                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
80                'message' => $soapFault->getMessage(),
81            ]));
82
83            throw new IntegrationSoapSeniorException(
84                __("Failure to consume Senior's soap: :message (:wsdl)", [
85                    'message' => $soapFault->getMessage(),
86                    'wsdl' => $wsdlURI,
87                ]),
88                previous: $soapFault
89            );
90        }
91    }
92
93    /**
94     * @return array
95     */
96    private function retrieveUserAndPassword(): array
97    {
98        $user = config('integration.user');
99        $password = config('integration.user_password');
100        $authUser = Auth::user();
101
102        // Se o usuario for requisitante, utiliza usuario e senha do ERP vinculados a ele.
103        if ($authUser?->isRequester()) {
104            $tokenId = $authUser->currentAccessToken()->id;
105
106            $user = $authUser->senior()['user'];
107            $password = Crypt::decryptString(Cache::get("erp_password_{$tokenId}"));
108        }
109
110        return ['user' => $user, 'password' => $password];
111    }
112}