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
|
30 |
|
public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler) |
53
|
|
|
{ |
54
|
30 |
|
$this->serviceDefinition = $serviceDefinition; |
55
|
30 |
|
$this->serializer = $serializer; |
56
|
|
|
|
57
|
30 |
|
$this->client = $client; |
58
|
30 |
|
$this->messageFactory = $messageFactory; |
59
|
30 |
|
$this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler); |
60
|
30 |
|
$this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap'])); |
61
|
30 |
|
} |
62
|
|
|
|
63
|
28 |
|
public function __call($functionName, array $args) |
64
|
|
|
{ |
65
|
28 |
|
$soapOperation = $this->findOperation($functionName, $this->serviceDefinition); |
66
|
28 |
|
$message = $this->argumentsReader->readArguments($args, $soapOperation['input']); |
67
|
|
|
|
68
|
28 |
|
$xmlMessage = $this->serializer->serialize($message, 'xml'); |
|
|
|
|
69
|
28 |
|
$headers = $this->buildHeaders($soapOperation); |
70
|
28 |
|
$request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage); |
71
|
|
|
|
72
|
|
|
try { |
73
|
28 |
|
$response = $this->client->sendRequest($request); |
74
|
18 |
|
if (strpos($response->getHeaderLine('Content-Type'), 'xml') === false) { |
75
|
2 |
|
throw new ServerException( |
76
|
2 |
|
$response, |
77
|
2 |
|
$request, |
78
|
2 |
|
"Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'" |
79
|
2 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// fast return if no return is expected |
83
|
16 |
|
if (!count($soapOperation['output']['parts'])) { |
84
|
4 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
12 |
|
$body = (string)$response->getBody(); |
88
|
12 |
|
if (strpos($body, ':Fault>')!==false) { |
89
|
|
|
$fault = $this->serializer->deserialize($body, Fault::class, 'xml'); |
90
|
|
|
throw new FaultException( |
91
|
|
|
$fault, |
92
|
|
|
$response, |
93
|
|
|
$request, |
94
|
|
|
"SOAP Fault", |
95
|
|
|
null, |
96
|
|
|
new \Exception() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
$response = $this->serializer->deserialize($body, $soapOperation['output']['message_fqcn'], 'xml'); |
101
|
24 |
|
} catch (HttpException $e) { |
102
|
|
|
|
103
|
11 |
|
if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'xml') !== false) { |
104
|
6 |
|
$fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), Fault::class, 'xml'); |
105
|
6 |
|
throw new FaultException( |
106
|
6 |
|
$fault, |
107
|
6 |
|
$e->getResponse(), |
108
|
6 |
|
$e->getRequest(), |
109
|
6 |
|
$e->getMessage(), |
110
|
6 |
|
null, |
111
|
|
|
$e |
112
|
6 |
|
); |
113
|
|
|
} else { |
114
|
4 |
|
throw new ServerException( |
115
|
4 |
|
$e->getResponse(), |
116
|
4 |
|
$e->getRequest(), |
117
|
4 |
|
$e->getMessage(), |
118
|
4 |
|
null, |
119
|
|
|
$e |
120
|
4 |
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
12 |
|
return $this->resultCreator->prepareResult($response, $soapOperation['output']); |
124
|
|
|
} |
125
|
|
|
|
126
|
28 |
|
protected function buildHeaders(array $operation) |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
28 |
|
'Content-Type' => isset($operation['version']) && $operation['version'] === '1.2' |
130
|
28 |
|
? 'application/soap+xml; charset=utf-8' |
131
|
28 |
|
: 'text/xml; charset=utf-8', |
132
|
28 |
|
'SoapAction' => '"' . $operation['action'] . '"', |
133
|
28 |
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
28 |
|
protected function findOperation($functionName, array $serviceDefinition) |
137
|
|
|
{ |
138
|
28 |
|
if (isset($serviceDefinition['operations'][$functionName])) { |
139
|
28 |
|
return $serviceDefinition['operations'][$functionName]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($serviceDefinition['operations'] as $opName => $operation) { |
143
|
|
|
if (strtolower($functionName) == strtolower($opName)) { |
144
|
|
|
return $operation; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
throw new ClientException("Can not find an operation to run $functionName service call"); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.