Passed
Push — master ( f78dc8...d3e9de )
by Carlos C
46s queued 12s
created

SoapCaller::call()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 6
rs 9.8333
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 */
21
    private $extraParameters;
22
23
    /** @var LoggerInterface */
24
    private $logger;
25
26 25
    public function __construct(SoapClient $soapClient, array $extraParameters = [])
27
    {
28 25
        $this->soapClient = $soapClient;
29 25
        $this->extraParameters = $extraParameters;
30 25
        $this->logger = new NullLogger();
31 25
    }
32
33
    private function soapClient(): SoapClient
34
    {
35
        return $this->soapClient;
36
    }
37
38 3
    public function extraParameters(): array
39
    {
40 3
        return $this->extraParameters;
41
    }
42
43
    public function call(string $methodName, array $parameters): stdClass
44
    {
45
        $finalParameters = $this->finalParameters($parameters);
46
        $soap = $this->soapClient();
47
        try {
48
            $result = $soap->__soapCall($methodName, [$finalParameters]);
49
            $this->logger->debug(strval(json_encode([
50
                $methodName => $this->extractSoapClientTrace($soap),
51
            ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
52
            return $result;
53
        } catch (Throwable $exception) {
54
            $this->logger->error(strval(json_encode(
55
                ['method' => $methodName, 'parameters' => $finalParameters] + $this->extractSoapClientTrace($soap),
56
                JSON_PRETTY_PRINT
57
            )));
58
            throw new RuntimeException(sprintf('Fail soap call to %s', $methodName), 0, $exception);
59
        }
60
    }
61
62
    protected function extractSoapClientTrace(SoapClient $soapClient): array
63
    {
64
        return [
65
            'request.headers' => @$soapClient->__getLastRequestHeaders(),
66
            'request.body' => @$soapClient->__getLastRequest(),
67
            'response.headers' => @$soapClient->__getLastResponseHeaders(),
68
            'response.body' => @$soapClient->__getLastResponse(),
69
        ];
70
    }
71
72
    public function finalParameters(array $parameters): array
73
    {
74
        return array_merge($parameters, $this->extraParameters());
75
    }
76
77
    public function setLogger(LoggerInterface $logger): void
78
    {
79
        $this->logger = $logger;
80
    }
81
}
82