1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient; |
4
|
|
|
|
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReader; |
6
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\ArgumentsReaderInterface; |
7
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler; |
8
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope\Messages\Fault as Fault11; |
9
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope12\Messages\Fault as Fault12; |
10
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ClientException; |
11
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\ServerException; |
12
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\SoapException; |
13
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Exception\UnexpectedFormatException; |
14
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreator; |
15
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Result\ResultCreatorInterface; |
16
|
|
|
use GoetasWebservices\SoapServices\SoapEnvelope; |
17
|
|
|
use Http\Client\Exception\HttpException; |
18
|
|
|
use Http\Client\HttpClient; |
19
|
|
|
use Http\Message\MessageFactory; |
20
|
|
|
use JMS\Serializer\Serializer; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Psr\Http\Message\RequestInterface; |
23
|
|
|
|
24
|
|
|
class Client |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Serializer |
28
|
|
|
*/ |
29
|
|
|
protected $serializer; |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $serviceDefinition; |
34
|
|
|
/** |
35
|
|
|
* @var HttpClient |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MessageFactory |
41
|
|
|
*/ |
42
|
|
|
protected $messageFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ResultCreatorInterface |
46
|
|
|
*/ |
47
|
|
|
private $resultCreator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ArgumentsReaderInterface |
51
|
|
|
*/ |
52
|
|
|
private $argumentsReader; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var RequestInterface |
56
|
|
|
*/ |
57
|
|
|
private $requestMessage; |
58
|
|
|
/** |
59
|
|
|
* @var ResponseInterface |
60
|
|
|
*/ |
61
|
|
|
private $responseMessage; |
62
|
|
|
|
63
|
36 |
|
public function __construct(array $serviceDefinition, Serializer $serializer, MessageFactory $messageFactory, HttpClient $client, HeaderHandler $headerHandler) |
64
|
|
|
{ |
65
|
36 |
|
$this->serviceDefinition = $serviceDefinition; |
66
|
36 |
|
$this->serializer = $serializer; |
67
|
|
|
|
68
|
36 |
|
$this->client = $client; |
69
|
36 |
|
$this->messageFactory = $messageFactory; |
70
|
36 |
|
$this->argumentsReader = new ArgumentsReader($this->serializer, $headerHandler); |
71
|
36 |
|
$this->resultCreator = new ResultCreator($this->serializer, !empty($serviceDefinition['unwrap'])); |
72
|
36 |
|
} |
73
|
|
|
|
74
|
34 |
|
public function __call($functionName, array $args) |
75
|
|
|
{ |
76
|
34 |
|
$soapOperation = $this->findOperation($functionName, $this->serviceDefinition); |
77
|
34 |
|
$message = $this->argumentsReader->readArguments($args, $soapOperation['input']); |
78
|
|
|
|
79
|
34 |
|
$xmlMessage = $this->serializer->serialize($message, 'xml'); |
|
|
|
|
80
|
34 |
|
$headers = $this->buildHeaders($soapOperation); |
81
|
34 |
|
$this->requestMessage = $request = $this->messageFactory->createRequest('POST', $this->serviceDefinition['endpoint'], $headers, $xmlMessage); |
82
|
|
|
|
83
|
|
|
try { |
84
|
34 |
|
$this->responseMessage = $response = $this->client->sendRequest($request); |
85
|
24 |
|
if (strpos($response->getHeaderLine('Content-Type'), 'xml') === false) { |
86
|
2 |
|
throw new UnexpectedFormatException( |
87
|
2 |
|
$response, |
88
|
2 |
|
$request, |
89
|
2 |
|
"Unexpected content type '" . $response->getHeaderLine('Content-Type') . "'" |
90
|
2 |
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// fast return if no return is expected |
94
|
22 |
|
if (!count($soapOperation['output']['parts'])) { |
95
|
4 |
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
18 |
|
$body = (string)$response->getBody(); |
99
|
|
|
|
100
|
18 |
|
$faultClass = $this->findFaultClass($response); |
101
|
|
|
|
102
|
18 |
|
if (strpos($body, ':Fault>') !== false) { // some server returns a fault with 200 OK HTTP |
103
|
1 |
|
$fault = $this->serializer->deserialize($body, $faultClass, 'xml'); |
104
|
1 |
|
throw $fault->createException($response, $request); |
105
|
|
|
} |
106
|
|
|
|
107
|
17 |
|
$response = $this->serializer->deserialize($body, $soapOperation['output']['message_fqcn'], 'xml'); |
108
|
30 |
|
} catch (HttpException $e) { |
109
|
10 |
|
if (strpos($e->getResponse()->getHeaderLine('Content-Type'), 'xml') !== false) { |
110
|
6 |
|
$faultClass = $this->findFaultClass($e->getResponse()); |
111
|
6 |
|
$fault = $this->serializer->deserialize((string)$e->getResponse()->getBody(), $faultClass, 'xml'); |
112
|
6 |
|
throw $fault->createException($e->getResponse(), $e->getRequest(), $e); |
113
|
|
|
} else { |
114
|
5 |
|
throw new ServerException( |
115
|
4 |
|
$e->getResponse(), |
116
|
4 |
|
$e->getRequest(), |
117
|
|
|
$e |
118
|
4 |
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
17 |
|
return $this->resultCreator->prepareResult($response, $soapOperation['output']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return RequestInterface|null |
126
|
|
|
*/ |
127
|
2 |
|
public function __getLastRequestMessage() |
128
|
|
|
{ |
129
|
2 |
|
return $this->requestMessage; |
130
|
|
|
} |
131
|
|
|
/** |
132
|
|
|
* @return ResponseInterface|null |
133
|
|
|
*/ |
134
|
2 |
|
public function __getLastResponseMessage() |
135
|
|
|
{ |
136
|
2 |
|
return $this->responseMessage; |
137
|
|
|
} |
138
|
|
|
|
139
|
24 |
|
protected function findFaultClass(ResponseInterface $response) |
140
|
|
|
{ |
141
|
24 |
|
if (strpos($response->getHeaderLine('Content-Type'), 'application/soap+xml') === 0) { |
142
|
13 |
|
return Fault12::class; |
143
|
|
|
} else { |
144
|
11 |
|
return Fault11::class; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
34 |
|
protected function buildHeaders(array $operation) |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
34 |
|
'Content-Type' => isset($operation['version']) && $operation['version'] === '1.2' |
152
|
34 |
|
? 'application/soap+xml; charset=utf-8' |
153
|
34 |
|
: 'text/xml; charset=utf-8', |
154
|
34 |
|
'SoapAction' => '"' . $operation['action'] . '"', |
155
|
34 |
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
34 |
|
protected function findOperation($functionName, array $serviceDefinition) |
159
|
|
|
{ |
160
|
34 |
|
if (isset($serviceDefinition['operations'][$functionName])) { |
161
|
33 |
|
return $serviceDefinition['operations'][$functionName]; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
foreach ($serviceDefinition['operations'] as $operation) { |
165
|
1 |
|
if (strtolower($functionName) == strtolower($operation['method'])) { |
166
|
1 |
|
return $operation; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
throw new ClientException("Can not find an operation to run $functionName service call"); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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.