Test Failed
Pull Request — master (#3)
by Carlos C
02:52
created

SoapCaller::finalParameters()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
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 */
21
    private $extraParameters;
22
23
    /** @var LoggerInterface */
24
    private $logger;
25
26 24
    public function __construct(SoapClient $soapClient, array $extraParameters = [])
27
    {
28 24
        $this->soapClient = $soapClient;
29 24
        $this->extraParameters = $extraParameters;
30 24
        $this->logger = new NullLogger();
31 24
    }
32
33 1
    private function soapClient(): SoapClient
34
    {
35 1
        return $this->soapClient;
36
    }
37
38 4
    public function extraParameters(): array
39
    {
40 4
        return $this->extraParameters;
41
    }
42
43 1
    public function call(string $methodName, array $parameters): stdClass
44
    {
45 1
        $finalParameters = $this->finalParameters($parameters);
46 1
        $soap = $this->soapClient();
47
        try {
48 1
            $result = $soap->__soapCall($methodName, [$finalParameters]);
49 1
            $this->logger->debug(strval(json_encode([
50 1
                $methodName => $this->extractSoapClientTrace($soap),
51 1
            ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
52 1
            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 1
    protected function extractSoapClientTrace(SoapClient $soapClient): array
63
    {
64
        return [
65 1
            'request.headers' => @$soapClient->__getLastRequestHeaders(),
66 1
            'request.body' => @$soapClient->__getLastRequest(),
67 1
            'response.headers' => @$soapClient->__getLastResponseHeaders(),
68 1
            'response.body' => @$soapClient->__getLastResponse(),
69
        ];
70
    }
71
72 1
    public function finalParameters(array $parameters): array
73
    {
74 1
        return array_merge($parameters, $this->extraParameters());
75
    }
76
77 1
    public function setLogger(LoggerInterface $logger): void
78
    {
79 1
        $this->logger = $logger;
80 1
    }
81
}
82