|
1
|
|
|
<?php |
|
2
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient; |
|
3
|
|
|
|
|
4
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReader; |
|
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReaderInterface; |
|
6
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler; |
|
7
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ClientException; |
|
8
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\FaultException; |
|
9
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ServerException; |
|
10
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\SoapException; |
|
11
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreator; |
|
12
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreatorInterface; |
|
13
|
|
|
use GoetasWebservices\SoapServices\SoapCommon as SoapCommon; |
|
14
|
|
|
use GoetasWebservices\SoapServices\SoapCommon\SoapEnvelope\Parts\Fault; |
|
15
|
|
|
use GoetasWebservices\SoapServices\SoapEnvelope; |
|
16
|
|
|
use Http\Client\Exception\HttpException; |
|
17
|
|
|
use Http\Client\HttpClient; |
|
18
|
|
|
use Http\Message\MessageFactory; |
|
19
|
|
|
use JMS\Serializer\Serializer; |
|
20
|
|
|
|
|
21
|
|
|
class Client |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Serializer |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $serializer; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $serviceDefinition; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var HttpClient |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $client; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var MessageFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $messageFactory; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ResultCreatorInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $resultCreator; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ArgumentsReaderInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $argumentsReader; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
16 |
|
public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler) |
|
53
|
|
|
{ |
|
54
|
16 |
|
$this->serviceDefinition = $serviceDefinition; |
|
55
|
16 |
|
$this->serializer = $serializer; |
|
56
|
|
|
|
|
57
|
16 |
|
$this->client = $client; |
|
58
|
16 |
|
$this->messageFactory = $messageFactory; |
|
59
|
16 |
|
$this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler); |
|
60
|
16 |
|
$this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap'])); |
|
61
|
16 |
|
} |
|
62
|
|
|
|
|
63
|
14 |
|
public function __call($functionName, array $args) |
|
64
|
|
|
{ |
|
65
|
14 |
|
$soapOperation = $this->findOperation($functionName, $this->serviceDefinition); |
|
66
|
14 |
|
$message = $this->argumentsReader->readArguments($args, $soapOperation['input']); |
|
67
|
|
|
|
|
68
|
14 |
|
$xmlMessage = $this->serializer->serialize($message, 'xml'); |
|
|
|
|
|
|
69
|
14 |
|
$headers = $this->buildHeaders($soapOperation); |
|
70
|
14 |
|
$request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
14 |
|
$response = $this->client->sendRequest($request); |
|
74
|
9 |
|
if (strpos($response->getHeaderLine('Content-Type'), 'text/xml') !== 0) { |
|
75
|
1 |
|
throw new ServerException( |
|
76
|
1 |
|
$response, |
|
77
|
2 |
|
$request, |
|
78
|
1 |
|
"Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'" |
|
79
|
1 |
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// fast return if no return is expected |
|
83
|
8 |
|
if (!count($soapOperation['output']['parts'])) { |
|
84
|
2 |
|
return null; |
|
85
|
|
|
} |
|
86
|
6 |
|
$response = $this->serializer->deserialize((string)$response->getBody(), $soapOperation['output']['message_fqcn'], 'xml'); |
|
87
|
12 |
|
} catch (HttpException $e) { |
|
88
|
|
|
|
|
89
|
5 |
|
if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'text/xml') === 0) { |
|
90
|
3 |
|
$fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), Fault::class, 'xml'); |
|
91
|
3 |
|
throw new FaultException( |
|
92
|
3 |
|
$fault, |
|
93
|
4 |
|
$e->getResponse(), |
|
94
|
3 |
|
$e->getRequest(), |
|
95
|
3 |
|
$e->getMessage(), |
|
96
|
3 |
|
null, |
|
97
|
|
|
$e |
|
98
|
3 |
|
); |
|
99
|
|
|
} else { |
|
100
|
2 |
|
throw new ServerException( |
|
101
|
2 |
|
$e->getResponse(), |
|
102
|
2 |
|
$e->getRequest(), |
|
103
|
2 |
|
$e->getMessage(), |
|
104
|
2 |
|
null, |
|
105
|
|
|
$e |
|
106
|
2 |
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
return $this->resultCreator->prepareResult($response, $soapOperation['output']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
14 |
|
protected function buildHeaders(array $operation) |
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
14 |
|
'Content-Type' => 'text/xml; charset=utf-8', |
|
117
|
14 |
|
'Soap-Action' => '"' . $operation['action'] . '"', |
|
118
|
14 |
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
14 |
|
protected function findOperation($functionName, array $serviceDefinition) |
|
122
|
|
|
{ |
|
123
|
14 |
|
if (isset($serviceDefinition['operations'][$functionName])) { |
|
124
|
14 |
|
return $serviceDefinition['operations'][$functionName]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
foreach ($serviceDefinition['operations'] as $opName => $operation) { |
|
128
|
|
|
if (strtolower($functionName) == strtolower($opName)) { |
|
129
|
|
|
return $operation; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
throw new ClientException("Can not find an operation to run $functionName service call"); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.