Passed
Push — main ( b480f2...a8d709 )
by Carlos C
02:53 queued 12s
created

SoapCaller::soapClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\Finkok;
6
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
use RuntimeException;
11
use SoapClient;
12
use stdClass;
13
use Throwable;
14
15
class SoapCaller implements LoggerAwareInterface
16
{
17
    /** @var SoapClient */
18
    private $soapClient;
19
20
    /** @var array<mixed> */
21
    private $extraParameters;
22
23
    /** @var LoggerInterface */
24
    private $logger;
25
26
    /**
27
     * @param SoapClient $soapClient
28
     * @param array<mixed> $extraParameters
29
     */
30 91
    public function __construct(SoapClient $soapClient, array $extraParameters = [])
31
    {
32 91
        $this->soapClient = $soapClient;
33 91
        $this->extraParameters = $extraParameters;
34 91
        $this->logger = new NullLogger();
35
    }
36
37 60
    private function soapClient(): SoapClient
38
    {
39 60
        return $this->soapClient;
40
    }
41
42
    /** @return array<mixed> */
43 63
    public function extraParameters(): array
44
    {
45 63
        return $this->extraParameters;
46
    }
47
48
    /**
49
     * @param string $methodName
50
     * @param array<mixed> $parameters
51
     * @return stdClass
52
     */
53 60
    public function call(string $methodName, array $parameters): stdClass
54
    {
55 60
        $finalParameters = $this->finalParameters($parameters);
56 60
        $soap = $this->soapClient();
57
        try {
58 60
            $result = $soap->__soapCall($methodName, [$finalParameters]);
59 60
            $this->logger->debug(strval(json_encode([
60 60
                $methodName => $this->extractSoapClientTrace($soap),
61 60
            ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
62
            /** @var stdClass $result */
63 60
            return $result;
64
        } catch (Throwable $exception) {
65
            $this->logger->error(strval(json_encode(
66
                ['method' => $methodName, 'parameters' => $finalParameters] + $this->extractSoapClientTrace($soap),
67
                JSON_PRETTY_PRINT
68
            )));
69
            throw new RuntimeException(sprintf('Fail soap call to %s', $methodName), 0, $exception);
70
        }
71
    }
72
73
    /**
74
     * @param SoapClient $soapClient
75
     * @return array<string, string>
76
     * @noinspection PhpUsageOfSilenceOperatorInspection
77
     */
78 60
    protected function extractSoapClientTrace(SoapClient $soapClient): array
79
    {
80 60
        return [
81 60
            'request.headers' => (string) @$soapClient->__getLastRequestHeaders(),
82 60
            'request.body' => (string) @$soapClient->__getLastRequest(),
83 60
            'response.headers' => (string) @$soapClient->__getLastResponseHeaders(),
84 60
            'response.body' => (string) @$soapClient->__getLastResponse(),
85 60
        ];
86
    }
87
88
    /**
89
     * @param array<mixed> $parameters
90
     * @return array<mixed>
91
     */
92 60
    public function finalParameters(array $parameters): array
93
    {
94 60
        return array_merge($parameters, $this->extraParameters());
95
    }
96
97 60
    public function setLogger(LoggerInterface $logger): void
98
    {
99 60
        $this->logger = $logger;
100
    }
101
}
102