Completed
Pull Request — master (#11)
by Carlos C
03:41
created

SoapCaller   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 22.58%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 65
ccs 7
cts 31
cp 0.2258
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extractSoapClientTrace() 0 7 1
A extraParameters() 0 3 1
A finalParameters() 0 3 1
A call() 0 16 2
A __construct() 0 5 1
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 */
21
    private $extraParameters;
22
23
    /** @var LoggerInterface */
24
    private $logger;
25
26 26
    public function __construct(SoapClient $soapClient, array $extraParameters = [])
27
    {
28 26
        $this->soapClient = $soapClient;
29 26
        $this->extraParameters = $extraParameters;
30 26
        $this->logger = new NullLogger();
31 26
    }
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