Passed
Push — master ( a8e753...d3e13e )
by Carlos C
05:06 queued 03:16
created

SoapCaller   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 83.87%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 84
ccs 26
cts 31
cp 0.8387
rs 10
c 4
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extraParameters() 0 3 1
A __construct() 0 5 1
A extractSoapClientTrace() 0 7 1
A finalParameters() 0 3 1
A call() 0 16 2
A soapClient() 0 3 1
A setLogger() 0 3 1
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 87
    public function __construct(SoapClient $soapClient, array $extraParameters = [])
31
    {
32 87
        $this->soapClient = $soapClient;
33 87
        $this->extraParameters = $extraParameters;
34 87
        $this->logger = new NullLogger();
35 87
    }
36
37 56
    private function soapClient(): SoapClient
38
    {
39 56
        return $this->soapClient;
40
    }
41
42
    /** @return array<mixed> */
43 59
    public function extraParameters(): array
44
    {
45 59
        return $this->extraParameters;
46
    }
47
48
    /**
49
     * @param string $methodName
50
     * @param array<mixed> $parameters
51
     * @return stdClass
52
     */
53 56
    public function call(string $methodName, array $parameters): stdClass
54
    {
55 56
        $finalParameters = $this->finalParameters($parameters);
56 56
        $soap = $this->soapClient();
57
        try {
58 56
            $result = $soap->__soapCall($methodName, [$finalParameters]);
59 56
            $this->logger->debug(strval(json_encode([
60 56
                $methodName => $this->extractSoapClientTrace($soap),
61 56
            ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
62 56
            return $result;
63
        } catch (Throwable $exception) {
64
            $this->logger->error(strval(json_encode(
65
                ['method' => $methodName, 'parameters' => $finalParameters] + $this->extractSoapClientTrace($soap),
66
                JSON_PRETTY_PRINT
67
            )));
68
            throw new RuntimeException(sprintf('Fail soap call to %s', $methodName), 0, $exception);
69
        }
70
    }
71
72
    /**
73
     * @param SoapClient $soapClient
74
     * @return array<string, string>
75
     * @noinspection PhpUsageOfSilenceOperatorInspection
76
     */
77 56
    protected function extractSoapClientTrace(SoapClient $soapClient): array
78
    {
79
        return [
80 56
            'request.headers' => @$soapClient->__getLastRequestHeaders(),
81 56
            'request.body' => @$soapClient->__getLastRequest(),
82 56
            'response.headers' => @$soapClient->__getLastResponseHeaders(),
83 56
            'response.body' => @$soapClient->__getLastResponse(),
84
        ];
85
    }
86
87
    /**
88
     * @param array<mixed> $parameters
89
     * @return array<mixed>
90
     */
91 56
    public function finalParameters(array $parameters): array
92
    {
93 56
        return array_merge($parameters, $this->extraParameters());
94
    }
95
96 56
    public function setLogger(LoggerInterface $logger): void
97
    {
98 56
        $this->logger = $logger;
99 56
    }
100
}
101