Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.18% covered (warning)
58.18%
32 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SeniorRequestExport
58.18% covered (warning)
58.18%
32 / 55
0.00% covered (danger)
0.00%
0 / 2
19.85
0.00% covered (danger)
0.00%
0 / 1
 execute
54.17% covered (warning)
54.17%
26 / 48
0.00% covered (danger)
0.00%
0 / 1
14.16
 retrieveUserAndPassword
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3namespace Domain\Shared\Integrations\Senior;
4
5use Domain\Request\DTOs\ExportRequestSeniorDTO;
6use Domain\Shared\Integrations\Senior\Exceptions\IntegrationSoapSeniorException;
7use Illuminate\Support\Facades\Auth;
8use SoapFault;
9
10use function __;
11
12class SeniorRequestExport extends SeniorImportBase
13{
14    /**
15     * @throws IntegrationSoapSeniorException
16     */
17    public function execute(ExportRequestSeniorDTO $exportRequestSeniorDTO): ?object
18    {
19        $wsdlURI = config('integration.url').'/sapiens_Synccom_senior_g5_co_mcm_est_requisicoes?wsdl';
20        $credentials = $this->retrieveUserAndPassword();
21        $payload = $exportRequestSeniorDTO->toArray();
22        $context = $this->makeSoapContext('requisicaoAgrupada', $wsdlURI, $payload);
23        $startedAt = microtime(true);
24        $user = $credentials['user'];
25        $password = $credentials['password'];
26
27        try {
28            $this->logSoapInfo('senior.soap.started', $context);
29
30            $client = new SeniorSoapClient($wsdlURI, $this->options);
31            $this->logSoapInfo('senior.soap.client_ready', array_merge($context, [
32                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
33            ]));
34
35            $response = $client->RequisicaoAgrupada(
36                $user,
37                $password,
38                0,
39                $payload
40            );
41
42            $this->logSoapInfo('senior.soap.finished', array_merge($context, [
43                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
44            ], $this->responseLogContext($response)));
45
46            $returnCode = $this->resolveSoapReturnCode($response);
47            if ((isset($response->erroExecucao) || isset($response->erros)) || ($returnCode !== null && $returnCode !== 0)) {
48                $this->logSoapError('senior.soap.functional_error', array_merge($context, [
49                    'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
50                ], $this->responseLogContext($response)));
51
52                throw new IntegrationSoapSeniorException(
53                    __("Senior's SOAP request returned with an error: :message (:wsdl)", [
54                        'message' => $this->resolveSoapErrorMessage($response),
55                        'wsdl' => $wsdlURI,
56                    ]),
57                );
58            }
59
60            $requestNumber = $this->resolveSoapRequestNumber($response);
61            if ($requestNumber && $requestNumber > 0) {
62                return $response;
63            }
64
65            return null;
66        } catch (SoapFault $soapFault) {
67            $this->logSoapError('senior.soap.soap_fault', array_merge($context, [
68                'elapsed_ms' => $this->elapsedMilliseconds($startedAt),
69                'message' => $soapFault->getMessage(),
70            ]));
71
72            throw new IntegrationSoapSeniorException(
73                __("Failure to consume Senior's soap: :message (:wsdl)", [
74                    'message' => $soapFault->getMessage(),
75                    'wsdl' => $wsdlURI,
76                ]),
77                previous: $soapFault
78            );
79        }
80    }
81
82    /**
83     * @return array
84     */
85    private function retrieveUserAndPassword(): array
86    {
87        $authenticatedUser = Auth::user();
88
89        if ($authenticatedUser && $authenticatedUser->hasWarehouseRequesterAccess()) {
90            return (new SeniorCredentialResolver)->resolveForAuthenticatedUser($authenticatedUser);
91        }
92
93        return [
94            'user' => (string) config('integration.user'),
95            'password' => (string) config('integration.user_password'),
96        ];
97    }
98}